NASutils Knowledge BaseUnofficialPolski

Bash · lesson 5 of 30 · First stepsUse exit codes

Every command ends with a code. && goes on after 0 and || after anything else.

Programming on the NAS › Bash in 30 lessons

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 3

Exercise

Count the errors only if the log exists.