Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture the exit code of a shell script in a perl script?

Tags:

shell

perl

i want to pop up an alert box using perl script. I am using exit 0 to terminate the shell script successfully and exit 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.

like image 557
Rahul Avatar asked Aug 26 '13 08:08

Rahul


People also ask

How do I exit a shell in Perl?

exit() function evaluates the expression passed to it and exits from the Perl interpreter while returning the value as the exit value. The exit() function does not always exit immediately but calls the end routines before it terminates the program.

How do I find the exit code for a script?

To check the exit code we can simply print the $? special variable in bash. This variable will print the exit code of the last run command. $ echo $?

How do I find my return code in Perl?

Examining exit code in Perl If you happen to execute one perl script from another, for example using the system function, Perl has the same variable $? containing the exit code of the "other program". The call to system will return the exit code and it will be also saved in the $? variable of Perl.

How do I print last exit code?

Extracting the elusive exit code To display the exit code for the last command you ran on the command line, use the following command: $ echo $?


1 Answers

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.

like image 57
TLP Avatar answered Oct 21 '22 16:10

TLP