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

Running shell commands from within Perl

Status
Not open for further replies.

sfouant

Technical User
Aug 27, 2008
1
US
Hi folks,

I'm trying to perform the following function within Perl:

@ret=system("/bin/rtr_cmd1 $rtr_name \"show bgp summary\" | sed -e 's/\([0-7]d\) \([0-9]\)/\1\2/gi ; s/Last Up/Last_Up/gi'");
print @ret;

When I run the command through the shell, as in:

shell# /bin/rtr_cmd1 rtreqab "show bgp summary" | sed -e 's/\([0-7]d\) \([0-9]\)/\1\2/gi ; s/Last Up/Last_Up/gi'

everything works fine - sed is properly manipulating the data stream and outputting it properly, however, within my perl script, sed does not seem to be working properly, i.e., it is not handling the stream in a similar manner as when I run the command straight from shell.

Any assistance is greatly appreciated!

Regards,

--
Stefan Fouant
Principal Network Engineer
NeuStar, Inc. - GPG Key ID: 0xB5E3803D
 
Are you trying to capture the command's output? If so, use backticks instead of system().

i.e.
Code:
my $ret = system("ls -hal /");
print "system: $ret\n\n";

my $ret = `ls -hal /`;
print "backticks: $ret\n\n";

system() only returns the exit status of the program (usually 0 on success or any other number on error/die/etc), whereas backticks return the STDOUT stream from the executed command (and sets $? or something to equal the exit code).

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Yea, look up the system() function, you will find its not the correct function to be using.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Try using:

use Shell(exec);

@ret=exec("/bin/rtr_cmd1 $rtr_name \"show bgp summary\" | sed -e 's/\([0-7]d\) \([0-9]\)/\1\2/gi ; s/Last Up/Last_Up/gi'");
print @ret;
 
Also it doesn't make much sense to | to sed when perl can do it much better.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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