sebastiangeiger
sebastiangeiger

Reputation: 3416

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.

Upvotes: 44

Views: 65852

Answers (3)

BIBS
BIBS

Reputation: 414

One method to implement this could be to use EOF tag and a master script which will create your my_notify script.


#!/bin/bash

if [ -f my_notify ] ; then
rm -rf my_notify
fi

if [ -f my_temp ] ; then
rm -rf my_temp
fi

retval=`ls non_existent_file &> /dev/null  ; echo $?`
ppid=$PPID
echo "retval=$retval" 
echo "ppid=$ppid" 
cat >> my_notify << 'EOF'
#!/bin/bash

echo "exit code: $retval"
echo " PPID =$ppid"
EOF

sh my_notify 

You can refine this script for your purpose.

Upvotes: 3

qqx
qqx

Reputation: 19475

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.

Upvotes: 45

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798744

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.

Upvotes: 6

Related Questions