Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Importing output from script

Status
Not open for further replies.

jawake

MIS
Jun 18, 2004
14
0
0
US
I'm a perl newbie, so pls be kind.

I've written a simple script that greps a log file for specific records. I want to take the output of this script and use it in a perl program. I can't seem to get this to work. I've tried the "system" and "qx//" functions unsuccessfully.

Any help appreciated.
 
jawake,

use the backtick operator to return the output of your script

Code:
$output=`your_script`;

if you're still having difficulties please post your code

 
If the script is small enough it might be worth putting everything into perl. Saves maintenance on different scripting languages.
 
Thanks for the feedback. Others have told me to do the whole thing in Perl too. Part of the goal of my project is for me to learn how to use different Perl functions. In this case I'm trying to learn how to have a Perl program call another app, take output from the app, and then do something with it.

I have a 1 line script called foobar that simply greps a file for "grep_app" (omiting anything with "images"):

grep -v images /var/log/httpd/access_log | grep foobar

I then wrote a short Perl program to print these results.

#!/usr/bin/perl
print "Content-type: text/html\n\n";
open(GREP_OUTPUT, "grep_app |");
while(<GREP_OUTPUT>)
{ $line = $_;
print $line; }
close GREP_OUTPUT;
exit;

For some reason this is not working as I expect it to. Any thoughts?
 
oops. that should read: I have a 1 line script called grep_app that greps for "foobar".
 
When you say it "doesn't work as you expect it to," what exactly do you mean? What happens when you try it?

Can you execute your 1-line grep script from the command line? How do you invoke it from there? What happens when you try that?
 
I can execute the one line script by typing ./grep_app from the command line. This produces about 200 lines of response.

When I execute the Perl code, all I get in response is:
Content-type: text/html

 
D'oh! Solved my own problem. I neglected the ./ in the "open" command. Thanks for the assistance. This is a great resource.
 
Glad you got it to work, but I'm surprised you need ./ before the script name.
If you just type "grep_app" at the command prompt without ./, what happens? (I'm thinking the script doesn't have execute permission set.)
 
I've set the file to CHMOD 755. If I just type grep_app at the command prompt I get:

bash: grep_app: command not found

If I type ./grep_app then it runs fine.
 
You are not trying to run the script via a cronjob are you? I know that if scripts are running from a cronjob this kind of problems occur. In normal operation it should see your script.

Just to make sure:
printenv PATH
 
No, I'm not using cron. A buddy of mine said I must not have "." in my path definition.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top