Exit status and chaining commands (&&, ||, ;)
You read the exit status ($?) every command leaves, make conditions with true, false and grep -q, chain commands conditionally with &&, || and ;, test files and numbers with [ ], see the difference between a subshell and grouping; at the end you run the server health checks, list the failing ones and build a safe deployment chain yourself. The failing checks differ in every lab.
Level 101 · 40 min
One of the nightly maintenance scripts carried on yesterday without noticing an error and copied new files over a half-finished backup. Daniel asks: "How does a script know that a command failed?"
The answer is the exit status: when a command finishes it leaves a number between 0 and 255. 0 means success, anything else means a problem. The number sits in the variable $? and lives only until the next command.
Analogy: every command leaves a note on the door when it finishes: "0: done" or "2: the file wasn’t there". && means "continue if the note says done", || means "use plan B if the note says trouble". The figure shows the chaining operators.
What you will do in this lesson
- Read the result with $?
- Commands that make conditions: true, false, grep -q
- && and ||
- ; carries on regardless
- Test: [ ] and test
- Health checks
- Subshell ( ) and grouping { }
- A safe deployment chain
- The list of broken services
- Short quiz
Skills you will earn
- Reading the exit status ($?)
- Chaining commands conditionally (&&, ||, ;)
- Testing files and numbers ([ ], test)
- Subshells and grouping ( ), { }
- Writing a short, precise finding
← Filtering and counting: grep, sort, uniq, wc, cut · Linux fundamentals · Variables, PATH and .bashrc →
Start for free