I like to know whether application is crashed or not from shell script. What would be the exit code if application crashed?
问题:
回答1:
The exit code of the application will be in the shell variable $?. If your application crashes, i.e. the operating system decides it has done something bad, and causes it to terminate (sends it a signal), then this is reflected in exit status $?.
Here is a simple function I use (in bash I set it as the PROMPT_COMMAND variable) put does some decoding of the exit status
check_exit_status ()
{
local status="$?";
local msg="";
local signal="";
if [ ${status} -ne 0 ]; then
if [ $((${status} < 128)) -ne 0 ]; then
msg="exit (${status})";
else
signal="$(builtin kill -l $((${status} - 128)) 2>/dev/null)";
if [ "$signal" ]; then
msg="kill -$signal$msg";
fi;
fi;
echo "[${status} => ${msg}]" 1>&2;
fi;
return 0
}
Hope you find it useful.
回答2:
Anything other than 0 indicates an error. Error values range from 1-255. Check them with $?
.
There are some exceptions to this, but 0 for success is the de facto standard on *nix.
回答3:
Normally the return code is 0 when nothing went wrong. You can check the return code with $?
fab@susi:~$ badCommand badCommand: command not found fab@susi:~$ fab@susi:~$ echo $? 127 fab@susi:~$ fab@susi:~$ whoami fab fab@susi:~$ fab@susi:~$ echo $? 0 fab@susi:~$
回答4:
Seems this is an answer to your question. The code snippet there demonstrates that the answer depends on the operating system (including Windows).