Next: , Previous: Esyscmd, Up: Shell commands


12.4 Exit status

To see whether a shell command succeeded, use sysval:

— Builtin: sysval

Expands to the exit status of the last shell command run with syscmd or esyscmd. Expands to 0 if no command has been run yet.

     syscmd(`false')
     =>
     ifelse(sysval, `0', `zero', `non-zero')
     =>non-zero
     syscmd(`exit 2')
     =>
     sysval
     =>2
     syscmd(`true')
     =>
     sysval
     =>0
     esyscmd(`false')
     =>
     ifelse(sysval, `0', `zero', `non-zero')
     =>non-zero
     esyscmd(`exit 2')
     =>
     sysval
     =>2
     esyscmd(`true')
     =>
     sysval
     =>0

sysval results in 127 if there was a problem executing the command, for example, if the system-imposed argument length is exceeded, or if there were not enough resources to fork. It is not possible to distinguish between failed execution and successful execution that had an exit status of 127.

On UNIX platforms, where it is possible to detect when command execution is terminated by a signal, rather than a normal exit, the result is the signal number shifted left by eight bits.

     dnl This test assumes kill is a shell builtin, and that signals are
     dnl recognizable.
     ifdef(`__unix__', ,
           `errprint(` skipping: syscmd does not have unix semantics
     ')m4exit(`77')')dnl
     syscmd(`kill -13 $$')
     =>
     sysval
     =>3328
     esyscmd(`kill -9 $$')
     =>
     sysval
     =>2304