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

difference between system function and backticks

Status
Not open for further replies.

rotis23

Programmer
Aug 29, 2002
121
0
0
GB
Is one *safer* than the other?

It's just quick and easy to call egrep/ls/cat sometimes rather than explicitly coding.
 
system() returns the exit code of the command you ran, whereas backticks return the command's output.
 
Hi,
I tried to retreive ncftpget ouptut using backticks in a variable or even in an array. The output of the command is displayed to STDOUT but nothing is put in the variable. What's wrong ?
Zephan
 
backticks capture the stdout of the command, you sure the information you want isn't coming back through stderr? If that's the case, try a bit of redirection and put 2>&1 after your command inside the backticks. Most shells support this.

________________________________________
Andrew - Perl Monkey
 
Thanks, icrf, actually it is my mistake when there are really an error. but the 2>&1 I added wont to print the :
/home/user/file06032004.txt: 1.48 kB 45.28 kB/s
output line when transfere is successfull.
 
Looks like qw can give more control than backticks?

"...You can't stop interpolation with backticks, but you can with qw." "...you can use single quotes to supress this.
 
Even better then backticks or system is using the appropriate perl commands to accomplish the task

Net::FTP for ftp related info

opendir() for ls style info

etc etc etc

Backticks and System are generally not the best if security and performance are concerns. I understand the need for it, I use when required, but if there is an alternative native perl method its best to go that route.
 
There's also another way...if you want to watch/parse the output of the executed command as it's happening and not just wait for it to end to get all the output (like backticks) you can use open...you can get the exit status, the output, and even a sub-process pid...it's the only way to go for things that may wedge or you want to see it as it happens...:

$pid_of_sub_process = open(CMD,"SomeCommandName |");
while(<CMD>) {
print "--> $_";
if (/Some Thing Which Means Its Messed Up/) {close CMD}
}
close(CMD);
$stat = $? >> 8; # did it exit with status?
if ($stat) {die "exited with status $stat"}
 
I will toss in my two cents here. I tend to look at things from a 'loaded server' perspective (~1 million hits a day +). If you are not anticipating high traffic opening named pipes works well.

But as load increases (~250k+ page views a day) named pipes will take a significant toll on your resources.

Thats been my experience. Your mileage may vary :) Sorry CadGuy, not saying your solution is not nice, its very nice, I've just been eaten by named pipes before so I am gunshy.

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top