Concept Flow - Error logging patterns
Start Script
Run Command
Check Exit Status
Log Error
End Script
The script runs a command, checks if it failed, logs an error if so, then continues or ends.
command_that_might_fail if [ $? -ne 0 ]; then echo "Error: command failed" >> error.log fi
| Step | Action | Command Exit Status ($?) | Condition | Log Action | Output |
|---|---|---|---|---|---|
| 1 | Run command_that_might_fail | 1 | 1 != 0 (True) | Append error message to error.log | error.log updated with 'Error: command failed' |
| 2 | Check condition again | 1 | No further action | No log | No change |
| 3 | End script | - | - | - | Script ends |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| $? | - | 1 | 1 | 1 |
| error.log | empty or previous content | added error message | unchanged | contains error message |
Error logging in bash: Run a command Check exit status with $? (0=success, non-zero=failure) If failure, append error message to a log file Use '>>' to keep previous logs This helps track failures without stopping script