LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to get exit code of a progarm in Shell? (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-get-exit-code-of-a-progarm-in-shell-365968/)

neo_in_matrix 09-22-2005 01:18 PM

How to get exit code of a progarm in Shell?
 
If want to know how to get the exit code of a program in shell scripts. Thanks.

Dark_Helmet 09-22-2005 01:56 PM

The exit code is stored in a special variable: $?

I would suggest storing the exit status in a variable immediately after executing the command to avoid any confusion. For instance:
Code:

#!/bin/bash

ls /opt > /dev/null
exit_status=$?

if [ ${exit_status} -eq 0 ] ; then
  echo "ls command completed successfully"
else
  echo "ls failed; exit status of ${exit_status}"
fi


neo_in_matrix 09-22-2005 02:46 PM

Wow, thanks!


All times are GMT -5. The time now is 10:32 PM.