How to get the return codes for shell commands

V

vabby

Hi
I am writing a perl script in which I am using some commnds like, mv,
rcp, rsync, rm etc. How can I get the return codes of these commands.

I read somewhere that the following will work.
my $result = 'mv abc abc.cfg';
But on actually trying this doesnot work. so I used the following

my $result =system('mv abc abc.cfg');

However for commands like rsync this is givin me erroneous results.

Tx
Vaibhav
 
V

vabby

Also for most of teh common commands the result code is 0. Is it also
true for other commands like rsync etc.
 
P

Paul Lalli

vabby said:
I am writing a perl script in which I am using some commnds like, mv,
rcp, rsync, rm etc. How can I get the return codes of these commands.

I read somewhere that the following will work.
my $result = 'mv abc abc.cfg';

Unlikely. Probably what you read was that the following will work:
my $result = `mv abc abc.cfg`;

That is, using back ticks, not single quotes. This is erroneous,
however. Backticks return the output of a command, not its exit
status.
But on actually trying this doesnot work. so I used the following

my $result =system('mv abc abc.cfg');

However for commands like rsync this is givin me erroneous results.

What exactly is erroneous about the results? Have you read the
documentation for the function you're using?

perldoc -f system

will tell you exactly what system() is returning, and how to get out of
that the actual exit status.

Paul Lalli
 
P

Paul Lalli

vabby said:
Also for most of teh common commands the result code is 0. Is it also
true for other commands like rsync etc.

Yes, and? What is making you believe the result code should *not* be
0? The vast majority of applications return 0 to indicate success.

Paul Lalli
 
A

Arved Sandstrom

Paul Lalli said:
Yes, and? What is making you believe the result code should *not* be
0? The vast majority of applications return 0 to indicate success.

Paul (and your tone is similar to the problem I have with Abigail), consider
this: a zero return is common with UNIX commands to indicate success. It is
*NOT* common in many programming paradigms that perhaps these posters are
used to, where you look for failure with a zero or FALSE. Depending on how
you define "application", it may well be that the vast majority of
"applications" do _not_ return 0 to indicate success; it's more natural to
return a non-zero.

But then again, we'd be talking non-UNIX, and of course everyone out there
is a UNIX guru.

AHS
 
G

grocery_stocker

vabby said:
Hi
I am writing a perl script in which I am using some commnds like, mv,
rcp, rsync, rm etc. How can I get the return codes of these commands.

I read somewhere that the following will work.
my $result = 'mv abc abc.cfg';
But on actually trying this doesnot work. so I used the following

my $result =system('mv abc abc.cfg');

However for commands like rsync this is givin me erroneous results.

Tx
Vaibhav

Maybe do something like the following in conjuction with IO::Select

#! /usr/bin/perl -w

use IPC::Open3;

local (*READ, *WRITE, *ERROR);

$pid = open3(\*READ, \*WRITE, \*ERROR, 'mv abc /efg');


#my $cmd = 'mv abc /efg';
#print READ "$cmd\n";

waitpid($pid, 0);
if($?) {
warn "exit code = ", $?>>8, "\n";
my $result = <ERROR>;
print $result;

}

cdalten@linux:~/perl> ./mv.pl
exit code = 1
mv: cannot stat `abc': No such file or directory
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top