You know && and ||. if does the same job with more room: many lines, and more than two choices.
bash
if grep -q ERROR ~/logs/till.log; then
echo "the till has errors"
else
echo "the till is fine"
figrep -q ERROR till.log
exit code 0
the then lines
grep -q ERROR till.log
any other code
the else lines
The parts
| Word | Means |
|---|---|
if COMMAND; then | Run COMMAND. If its exit code is 0, run the lines after then |
elif COMMAND; then | If the parts above did not run, try this command (optional) |
else | If no part above ran, run these lines (optional) |
fi | The end of the if |
Bash checks from the top and runs only the first part whose command works.
The lines inside are moved to the right with two spaces. Bash does not need it, but people can read the script more easily.
In a script, with exit
bash
#!/bin/bash
if grep -q ERROR ~/logs/till.log; then
echo "errors found"
exit 1
fi
echo "no errors"exit 1 ends the script with a problem code, so that a program that runs the script knows that the check failed.