Exit Status/Return Code
- every command returns an exit status
- integers ranging from 0 to 255
- 0 = success
- non 0 = error
- use for error checking
- have to use
manorinfofor the meaning of the error code
$?contains the return code of the previously executed command
eg
#!/bin/bash
HOST="archlinux.org"
ping -c 1 $HOST
if [ "$?" -eq "0" ]
then
echo "$HOST reachable"
else
echo "$HOST unreachable"
fiChaining Commands
- && = AND
mkdir /tmp/bak && cp test.txt /tmp/bak- only runs command only if returns
0
- only runs command only if returns
- || = OR
cp test.txt /tmp/bak || cp test.txt /tmp- only runs command only if does not return
0
- only runs command only if does not return
- ;
- all commands get executes
Exit Command
- explicitly define the return code
exit 0; ... ; exit 255
- the default value is that of the last command executed
eg
#!/bin/bash
HOST="archlinux.org"
ping -c 1 $HOST
if [ "$?" -eq "0" ]
then
echo "$HOST reachable"
else
echo "$HOST unreachable"
exit 1
fi
exit 0