Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Get return value from system ( ) ...?

Posted on 2000-02-21
17
128 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
ID: 2545478
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
ID: 2546212
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
ID: 2546334
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

by:elpmet
ID: 2546444
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
ID: 2546519
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
ID: 2547101
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
ID: 2547152
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
ID: 2547187
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
 
LVL 2

Expert Comment

by:mattrope
ID: 2547201
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
ID: 2547319
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
ID: 2547372
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
ID: 2547415
#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
ID: 2547430
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
ID: 2547435
I'm sorry I accidentally re-posted my previous comment.  My apology to all!

Elpmet
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2547465
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
ID: 2552589
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
ID: 2552602
no problem :-). I am glad that you got your answer, no matter from whom.

Regards
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

790 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