AITHA, BHEEMSEN (SBCSI) wrote:
> Hi,
>
> I am executing a shell script using the 'system' command from a PERL
> script. I want to grab the return code(output) from the shell script and
> validate it in my PERL script. Cam someone please explain me how to do
> that.
The actual return code is shited left 8 bits and returned from the system
call.
perlfunc man page:
system LIST
In general, do not assume the UNIX/POSIX semantics that you can
shift $? right by eight to get the exit value, or that "$? & 127"
would give you the number of the signal that terminated the program,
or that "$? & 128" would test true if the program was terminated by
a coredump. Instead, use the POSIX W*() interfaces: for example, use
WIFEXITED($?) and WEXITVALUE($?) to test for a normal exit and the
exit value, WIFSIGNALED($?) and WTERMSIG($?) for a signal exit and
the signal. Core dumping is not a portable concept, so there's no
portable way to test for that.
Only implemented if ToolServer is installed. (Mac OS)
As an optimization, may not call the command shell specified in
$ENV{PERL5SHELL}. "system(1, @args)" spawns an external process and
immediately returns its process designator, without waiting for it
to terminate. Return value may be used subsequently in "wait" or
"waitpid". Failure to spawn() a subprocess is indicated by setting
$? to "255 << 8". $? is set in a way compatible with Unix (i.e. the
exitstatus of the subprocess is obtained by "$? >> 8", as described
in the documentation). (Win32)
So this would get the return code from the spawned process:
my $ret = system $cmd;
my $rc = ($ret >> 8) & 0xff;
You should probably use the POSIX calls above to be more portable and to
handle any future changes.
my $rc = WEXITVALUE($?); # $? also contains the system return code
A return of 0 from an exiting process is successful.
--
,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED]
(_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED]
/ ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs