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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute shell commands, capture output problem

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
US
All,

I have the following snippet of perl code:
Code:
@results = `make abc.exe 2>&1 | grep -P '((undefined reference to|\.exe)\s)'`;
This command works fine from the shell prompt, but within my perl script nothing is returned into @results?? Any ideas on why this is the case...and what to do about it?

Thanks in advance!
 
Give full path's to everything to start with. Full path to make, abc.exe, grep etc..


Travis

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Tried adding the paths...still doesn't work. FYI: The code above works fine until I add the "| grep ....". That is, the make output lines get put into the array just fine without the pipe portion. Any other ideas?
 
you probably need to escape out your \'s. Perl see's one \ and assumes you are trying to escape what is after it to try \\ or \\\ I can't remember :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
That was it!! Here is the WORKING code:
Code:
@results = `make abc.exe 2>&1 | grep -P '((undefined reference to|\\.exe)\\s)'`;

Thanks for your help!!
 
i still say put the full paths to everything :)

I had a whole site full of scripts and the user they ran as somehow had it's paths blow away.. things went CRAZY!! :)

I full path everything now.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
But, what if the paths change...i.e., you run the script on a different platform? You may have to go thru and change all the paths, correct? If you leave the paths off...you should be good to go on any platform, correct?
 
no.. because even on a lot of platforms you might call ssh or something and there could be different versions (We have like 5 versions of ssh on one server for some reason). I don't want to depend on the fact that the user it is running on "happens" to have the paths I want.

At the beginning of my scripts I have a lot of things like
my $gzip = '/usr/bin/gzip';
my $gzcat = '/usr/bin/gzcat';
my $grep = '/usr/bin/grep';
my $ssh = '/usr/bin/ssh';

When I have to move from solaris to linux all I usually need to do is check and make sure they exist. For example on linux you normally don't have gzcat but you have zcat so I don't have to change my whole script, I just change the path for $gzcat.

All I can say is it has caused me more trouble not doing it, then doing it :D

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top