How to capture the exit code of a shell script in a perl script?
Solution 1
You can check the exit code of another process with the child error variable $?
. For example:
system("perl foo.pl");
my $exit_val = $? >> 8; # now contains the exit value of the perl script
Read the documentation for more info.
Solution 2
In case of exit 0:- shell script returns 0 to perl script $? variable
but for exit 1 case:-it return 256 so needed to be shifted by 8 therefore try this:
#!/usr/bin/perl
print "pelr";
system("./shell.sh");
$p=$?>>8;
print $p;
NOTE- in shell script just put exit 0 and run and then exit 1. and see the o/p
Just a note, when using system in perl, it returns the exit code multiplied by 256. So, if a command returns 1, system("command") will return 256. So, to get the real return value, divide by 256.
Related videos on Youtube
Rahul
Updated on June 04, 2022Comments
-
Rahul over 1 year
i want to pop up an alert box using perl script. I am using
exit 0
to terminate the shell script successfully andexit 1
to terminate the shell script when an error occurs. I want to capture this exit code in the perl script. And depending on the value 0 or 1, I want to pop up an alert box with success or failure message respectively. -
Rahul about 10 yearsi used your method, but its giving 127. but i am using exit 0 in my shell script. so, i need a 0 instead of 127.
-
Zaid about 10 years@Rahul :
$? & 127
will give you what you want, as explained in the linked documentation. -
Rahul about 10 yearsok i got. now how can i pop up a alert box in the perl script or using javascript inside perl.