Solved

Get return value from system ( ) ...?

Posted on 2000-02-21
17
125 Views
Last Modified: 2013-12-25
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
Comment
Question by:elpmet
  • 7
  • 6
  • 2
  • +1
17 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
 
LVL 1

Author Comment

by:elpmet
Comment Utility
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
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
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
 
LVL 1

Author Comment

by:elpmet
Comment Utility
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
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
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
 
LVL 2

Accepted Solution

by:
mattrope earned 50 total points
Comment Utility
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
 
LVL 1

Author Comment

by:elpmet
Comment Utility
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
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 2

Expert Comment

by:mattrope
Comment Utility
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
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
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
 
LVL 1

Author Comment

by:elpmet
Comment Utility
maneshr,

I have run your code,

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

but the $var does not contain the command output.

Elpmet.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
#if you want the stderr output of the command in $var instead of in /tmp/err
$var=`kill 1 2>&1`;
0
 
LVL 1

Author Comment

by:elpmet
Comment Utility
maneshr,

I have run your code,

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

but the $var does not contain the command output.

Elpmet.
0
 
LVL 1

Author Comment

by:elpmet
Comment Utility
I'm sorry I accidentally re-posted my previous comment.  My apology to all!

Elpmet
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
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
 
LVL 1

Author Comment

by:elpmet
Comment Utility
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
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
no problem :-). I am glad that you got your answer, no matter from whom.

Regards
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this tutorial I will aim to show you how simple is making a small application in WhizBase, how to add, remove and update data in the DB. I will make a small address book application where you can add, browse, update and remove addresses. I wi…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now