Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting exit code of last shell command in another script

I am trying to beef up my notify script. The way the script works is that I put it behind a long running shell command and then all sorts of notifications get invoked after the long running script finished.

For example:

sleep 100; my_notify 

It would be nice to get the exit code of the long running script. The problem is that calling my_notify creates a new process that does not have access to the $? variable.

Compare:

~ $: ls nonexisting_file; echo "exit code: $?"; echo "PPID: $PPID" ls: nonexisting_file: No such file or directory exit code: 1 PPID: 6203 

vs.

~ $: ls nonexisting_file; my_notify ls: nonexisting_file: No such file or directory exit code: 0 PPID: 6205 

The my_notify script has the following in it:

#!/bin/sh echo "exit code: $?" echo "PPID: $PPID" 

I am looking for a way to get the exit code of the previous command without changing the structure of the command too much. I am aware of the fact that if I change it to work more like time, e.g. my_notify longrunning_command... my problem would be solved, but I actually like that I can tack it at the end of a command and I fear complications of this second solution.

Can this be done or is it fundamentally incompatible with the way that shells work?

My shell is Z shell (zsh), but I would like it to work with Bash as well.

like image 702
sebastiangeiger Avatar asked Oct 21 '12 16:10

sebastiangeiger


People also ask

How do you get the last code out of Exit command?

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 $? The displayed response contains no pomp or circumstance. It's simply a number.

How do I find my shell exit code?

Checking Bash Exit Code Launch a terminal, and run any command. Check the value of the shell variable “$?” for the exit code. $ echo $? As the “date” command ran successfully, the exit code is 0.

Which command is used to display the exit code of the previous command?

The echo command is used to display the exit code for the last executed Fault Management command.

Is exit status of last command?

variable in Linux. “$?” is a variable that holds the return value of the last executed command. “echo $?” displays 0 if the last command has been successfully executed and displays a non-zero value if some error has occurred.


2 Answers

You'd really need to use a shell function in order to accomplish that. For a simple script like that it should be pretty easy to have it working in both zsh and bash. Just place the following in a file:

my_notify() {   echo "exit code: $?"   echo "PPID: $PPID" } 

Then source that file from your shell startup files. Although since that would be run from within your interactive shell, you may want to use $$ rather than $PPID.

like image 150
qqx Avatar answered Oct 05 '22 20:10

qqx


It is incompatible. $? only exists within the current shell; if you want it available in subprocesses then you must copy it to an environment variable.

The alternative is to write a shell function that uses it in some way instead.

like image 38
Ignacio Vazquez-Abrams Avatar answered Oct 05 '22 20:10

Ignacio Vazquez-Abrams