I'm trying to run perl script through bash, and get the perl's exit value.
perl_script.pl
print "test1";
sub a{
my @array = ("a","b");
if ($#array ne -1){
return 1;
}
else {return 0;}
}
my $result=a(arg1,arg2);
exit $result;
bash.sh
VARIABLE_1=$("perl_script.pl" arg1 arg2)
RESULT=$?
The '$?' variable keeps returning 0, no matter the exit value is. Do you know another way to retrieve the perl exit value from bash?
Thanks in advance!
bash's $?
will be set to the value passed to exit
[1] .
$ perl -e'exit 3'
$ echo $?
3
$ perl -e'exit 4'
$ echo $?
4
$ perl perl_script.pl
test1
$ echo $?
1
exit
, the exit code will by set to zero.I noticed the root cause of this problem. It is a simple issue but tricky. So I would like to share it here.
bash.sh
VARIABLE_1=$("perl_script.pl" arg1 arg2)
echo $?
RESULT=$?
The echo process has accessed the $?, so the RESULT variables would save the exit value of the echo operation which always turns 0 (successful operation)
The fix
VARIABLE_1=$("perl_script.pl" arg1 arg2)
RESULT=$?
echo $RESULT
As the conclusion, the $? has one-time use only right after the script executed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.