Exit status codes are used to terminate the script with the exit command. The exit command can be passed an integer value of the exit code to return from the script to the shell.
Example:
script1.sh
#!/bin/sh
FILENAME=/path/to/file
if [ -a $FILENAME]
exit 0
else
exit 1
fi
script2.sh
#!/bin/sh
script1.sh
if [ $? eq 1 ]
echo "Couldn't find required file"
exit 1
fi
In this example we wrote two scripts. Script1 simply checks for the existance of a file and if the file is found exits with exit code 0 and if the file is not found exits with exit code 1. Script2 checks the exit code which is stored in $?. If exit code 1 was returned the script echos that the file couldn't be found and exits with exit code 1.
Case statements work much like if statements except case statement can only interpret one statement against multiple conditions. Case statements are closed with esac or case backwards.
Example:
#!/bin/bash
case $ARGV[1] in
country)
echo "USA"
;;
state)
echo "Texas"
;;
*)
echo "Who knows"
;;
esac
In this example the variable $ARGV[1] (first command line option) is processed with a case statement. If "scriptname country" is executed USA is echoed back. If "scriptname state" is run Texas is echoed back. Anything else entered after the script name Who Knows is echoed back to the terminal.