Solved

Get return value from system ( ) ...?

Posted on 2000-02-21
17
126 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
 
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
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 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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Do you have experts in the field of AutoIt coding? 25 114
Simple JavaScript form question 4 64
jQuery Generalization 3 63
Autoit restart command not working 6 58
Ever wondered how to display how many visitors you have online. In this tutorial I will show you an easy but effective way to display the number of online visitors in WhizBase. In this article I assume you have read my previous articles and know …
Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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…

911 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

20 Experts available now in Live!

Get 1:1 Help Now