Bash · lesson 5 of 30 · First stepsUse exit codes
Every command ends with a code. && goes on after 0 and || after anything else.
05-exit-codes.sh Download 05-exit-codes.sh
#!/usr/app/TurboShell/bin/bash
# Every command ends with a code: 0 is fine, anything else is not
grep -q ERROR nas.log
echo "grep ERROR: $?"
grep -q DEBUG nas.log
echo "grep DEBUG: $?"
grep -q ERROR nas.log && echo "the log has errors"
grep -q DEBUG nas.log || echo "the log has no DEBUG lines"
exit 3
Terminal
$ bash 05-exit-codes.sh; echo "exit code $?"
grep ERROR: 0
grep DEBUG: 1
the log has errors
the log has no DEBUG lines
exit code 3Exercise
Count the errors only if the log exists.