Solved

Get return value from system ( ) ...?

Posted on 2000-02-21
17
130 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
[X]
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
  • 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
Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

632 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