Bash if Statement
An if statement runs commands only when a condition is true.
Add elif for more conditions and else for when none match. The statement ends with fi.
if, elif and else
Save this as grade.sh:
#!/bin/bash
score=72
if [ $score -ge 90 ]; then
echo "Excellent"
elif [ $score -ge 60 ]; then
echo "Passed"
else
echo "Failed"
fi
Run it:
./grade.sh
Passed
Test a command
if can test any command: it checks the exit status.
if grep -q "error" app.log; then echo "Errors found"; fi
Errors found