Search code examples
shellcshexit-code

How to capture exit code of a perl script which is called from a shell script


I have this sample perl code, where $a $b $c values can be changed and has exit code $ex. I call this from another shell script, which is affected by abc.pl's exit code. How do I capture just the exit code of this perl.

my $a=0; my $b=0; my $c=0;
my $sum = $a+$b+$c; $ex = 0;
if($sum == 0){
    print "success"; print " $ex \n";
}else{
    $ec=1 ; print "Not success"; print " $ex \n";
}
exit $ex;

Sample shell script.

echo start
set ecd = `abc.pl`
echo $ecd
echo stop

Here $ecd prints whatever is the output of abc.pl, but not just the exit code.

Now I have problem with if else loop,

set ecd = `abc.pl`
set status = $?
echo $status
if ( $status == 0 ) then
    echo "Here Status is $status"
else
    echo "Status is $status"
endif
echo $status

The value of status seems to change before and after the if loop, everytime it prints only the msg in if condition. Sample output. 1 Here Status is 0 0 Please help to correct this. Please suggest me how I can get better with the basics aswell. Help is very much appreciated. Thanks a lot in advance.


Solution

  • $? contains the exit code of the last executed command.

    set ecd = `abc.pl`
    set status = $?