Shouting the return code. (Re: Meaning of "rc" in cron/log)

Alexander Dupuy dupuy at douglass.columbia.edu
Fri Jul 1 08:00:30 AEST 1988


In <3671 at psuvax1.cs.psu.edu>, flee at gondor.cs.psu.edu (Felix Lee) writes:
> Does ksh let you put the return code in the prompt? Something like
> PS1='($?) '?
...
>  Showing only non-zero return codes would be better.

Here's what I do in my .kshrc ($ENV) file:

 status()
 {
   integer status=$?
   if [ $status -gt 128 -a $status -lt 160 ]; then
     echo "[Signal `expr $status - 128`]"
   else
     echo "[Status $status]"
   fi
 }

 trap 'status 1>&2' ERR			# don't let commands fail silently

With this, if any command exits with non-zero status, I see what it is.  If
there's no display, I know the status code was 0.

Something similar works for csh or SVR2 sh, but there is no ERR trap, so you
have to invoke it manually after every command.

SVR2 sh:

 status()
 {
   status=$?
   if [ $status -gt 128 -a $status -lt 160 ]; then
     echo "[Signal `expr $status - 128`]"
   else
     echo "[Status $status]"
   fi
   unset status
 }

csh:

 alias status	'echo "[Status $status]"'

@alex
-- 
inet: dupuy at columbia.edu
uucp: ...!rutgers!columbia!dupuy



More information about the Comp.unix.questions mailing list