How to capture shell exit code in perl script?

I want to open a warning window with a perl script. I use exit 0 to successfully complete the shell script and exit 1 to complete the shell script when an error occurs. I want to write this exit code in a perl script. And depending on the value 0 or 1, I want to display a warning window with a message about successful completion or error.

+4
source share
2 answers

Can you check the exit code of another process with the $? child error variable $? . For instance:

 system("perl foo.pl"); my $exit_val = $? >> 8; # now contains the exit value of the perl script 

Read the documentation for more details.

+8
source

In case of exit 0: - does the shell script return 0 in perl script $? Variable

but for the case with output 1: - return 256 so that it needs to be shifted by 8 so try the following:

 #!/usr/bin/perl print "pelr"; system("./shell.sh"); $p=$?>>8; print $p; 

NOTE. In the shell script, just type exit 0 and run and then exit 1. and look o / p

Just note that when using the system in perl, it returns the exit code multiplied by 256. Thus, if the command returns 1, the system ("command") will return 256. So, to get the actual return value, divide by 256.

+6
source

Source: https://habr.com/ru/post/1498919/


All Articles