Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 135
  • Last Modified:

Get return value from system ( ) ...?

Hi folks!

Is there any way to get a return value from system( ) function in Perl?

I'm hoping to avoid reinventing the wheel by hopefully calling shell commands from, and processing such inputs with my perl scripts.

(ex. system(traceroute www.perl.org);  system(du -sk ~); etc...)

Elpmet.
0
elpmet
Asked:
elpmet
  • 7
  • 6
  • 2
  • +1
1 Solution
 
ozoCommented:
The return value is the exit status of the program as returned by the wait() call.  To get the actual exit value divide by 256.
0
 
elpmetAuthor Commented:
Ozo, thanks for your comment.  I guess I asked my question wrong.


I'm interested in redirecting the output into my calling perl script, not to the STDOUT.

For instance, if I do such as
   system(kill 1);  ... which spits out "1: Not owner"
is there any way for Perl to capture such a command output ?

Elpmet.
0
 
maneshrCommented:
try this...
all that i am doing is redirecting the error to a temporary file and then checking if it exists. if it does that means the command did generate some error.

All that i do then is, display the file!!


You could use system too instead of `.

#!/usr/local/bin/perl

`rm /tmp/err 2> /dev/null`;  ## Remove any previous error file
$var=`kill 1 2> /tmp/err`;  ## Redirect the error to the temp file

if (-e "/tmp/err"){
  open(ERR,"/tmp/err") || die $!;
  print "Here's your error!\n";
  while(<ERR>){
    print $_;
  }
  close(ERR);
}
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
elpmetAuthor Commented:
maneshr,

I thought about dumping the output to a file and examining it, but am still wondering if I can accomplish this by dealing with memory only, not with slow disk.

Elpmet.
0
 
maneshrCommented:
do you expect to execute the command many many times??
if so disk access might be an overhead.

i think under the circumstance, the above is an easier way of implementing what you need.
0
 
mattropeCommented:
You can get the output of a command by invoking it with the backticks rather than system.  So

  $myname = `whoami`;

would capture your user name in the variable $myname.  Make sure you use backticks (the same key as ~) rather than normal apostrophes.

Hope this is what you were looking for.
0
 
elpmetAuthor Commented:
Thank you, everyone who tried to help me with this.

And, thank you, mattrope, it's just the answer I was looking for.

Elpmet.
0
 
maneshrCommented:
Hmm, isnt that the same answer that i had give??

check $var=`kill 1 2> /tmp/err`;  ## Redirect the error to the temp file

if you are not sure :)
0
 
mattropeCommented:
maneshr--there's no need to redirect the output into a temp file.  The contents of $var in your example hold everything that is necessary.  Hope that clears up the confusion.
0
 
maneshrCommented:
sure all that i am redirecting to the temp file is the ERROR and not the out put of that command. The output of that command is still stored in $var.

try running any command (kill 1 for eg.) from within system and see if the $var can tell you if an error has occured.

my script simply takes care of the scenario where an error might occur.

so i have in fact given an additional facility (which the user may or may not want to retain).
0
 
elpmetAuthor Commented:
maneshr,

I have run your code,

  $var=`kill 1 2> /tmp/err`;

but the $var does not contain the command output.

Elpmet.
0
 
ozoCommented:
#if you want the stderr output of the command in $var instead of in /tmp/err
$var=`kill 1 2>&1`;
0
 
elpmetAuthor Commented:
maneshr,

I have run your code,

  $var=`kill 1 2> /tmp/err`;

but the $var does not contain the command output.

Elpmet.
0
 
elpmetAuthor Commented:
I'm sorry I accidentally re-posted my previous comment.  My apology to all!

Elpmet
0
 
maneshrCommented:
you will only get something in $var if your command has been executed successfully. if your command has been unsuccessfull, the temporary file will be created.

kill 1 does not complete successfully, so $var is empty.

instead of kill 1 try ls .

0
 
elpmetAuthor Commented:
meneshr,

I see, I didn't try other commands.
Although I now understand that you knew the answer, I think I was somehow misled and my attention was focused to your temp-file solution.

I was interested in a solution dealing with memory only (not with disk drive), and you gave me a solution which dealt with both memory and a file.

But I still think that the redirecting the command output to a file is not necessary while we can get the result directly into a variable in perl script (as mattrope proposed), which is a lot faster and is the exact answer that I was looking for.

meneshr, please don't get me wrong.  I still appreciate your solution from which I was able to learn more (as you know, I'm a newbie in perl).  And after all, to me, learning is the whole point of browsing this site.

If I offended you in any way, then I must apologize.  But I had to give the points to mattrope who I think gave me the most clear-cut answer I was looking for.

Anyway, let me thank you for the series of your help, and I'm sure that we will talk again in other topic areas when I post my questions.

Elpmet
0
 
maneshrCommented:
no problem :-). I am glad that you got your answer, no matter from whom.

Regards
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now