Process Management
Command Status
Branching on Success
Every command exits with a numeric status. Scripts can test that status to choose the next action.
Program
Play the script to search fixed text for a selected word and label the result.
command_status.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
data=$'ok\nwarn\nfail'
needle="ok"
if printf '%s\n' "$data" | grep -q "$needle"; then
label="found"
else
label="missing"
fi
echo "$needle:$label"
#!/usr/bin/env bash
data=$'ok\nwarn\nfail'
needle="warn"
if printf '%s\n' "$data" | grep -q "$needle"; then
label="found"
else
label="missing"
fi
echo "$needle:$label"
#!/usr/bin/env bash
data=$'ok\nwarn\nfail'
needle="missing"
if printf '%s\n' "$data" | grep -q "$needle"; then
label="found"
else
label="missing"
fi
echo "$needle:$label"
data ← ok, warn, fail
3data=$'ok\nwarn\nfail'4needle="ok"values this stepok, warn, faildataneedle ← ok
3data=$'ok\nwarn\nfail'4needle="ok"5if printf '%s\n' "$data" | grep -q "$needle"; thenvalues this stepokneedlegrep status ← 0
4needle="ok"5if printf '%s\n' "$data" | grep -q "$needle"; then6 label="found"values this step0grep statusokneedlelabel ← found
5if printf '%s\n' "$data" | grep -q "$needle"; then6 label="found"7elsevalues this stepfoundlabelecho "$needle:$label"
9fi10echo "$needle:$label"outputok:foundvalues this stepokneedlefoundlabel
data ← ok, warn, fail
3data=$'ok\nwarn\nfail'4needle="warn"values this stepok, warn, faildataneedle ← warn
3data=$'ok\nwarn\nfail'4needle="warn"5if printf '%s\n' "$data" | grep -q "$needle"; thenvalues this stepwarnneedlegrep status ← 0
4needle="warn"5if printf '%s\n' "$data" | grep -q "$needle"; then6 label="found"values this step0grep statuswarnneedlelabel ← found
5if printf '%s\n' "$data" | grep -q "$needle"; then6 label="found"7elsevalues this stepfoundlabelecho "$needle:$label"
9fi10echo "$needle:$label"outputwarn:foundvalues this stepwarnneedlefoundlabel
data ← ok, warn, fail
3data=$'ok\nwarn\nfail'4needle="missing"values this stepok, warn, faildataneedle ← missing
3data=$'ok\nwarn\nfail'4needle="missing"5if printf '%s\n' "$data" | grep -q "$needle"; thenvalues this stepmissingneedlegrep status ← 1
4needle="missing"5if printf '%s\n' "$data" | grep -q "$needle"; then6 label="found"values this step1grep statusmissingneedlelabel ← missing
7else8 label="missing"9fivalues this stepmissinglabelecho "$needle:$label"
9fi10echo "$needle:$label"outputmissing:missingvalues this stepmissingneedlemissinglabel
Follow the Status
datacontainsok,warn, andfail.needlestarts asok.grep -q "$needle"findsok, so its status is0.- The
thenbranch setslabeltofound. - The script prints
ok:found. | needle | grep status | label | output | | --- | --- | --- | --- | | ok | 0 | found | ok:found | | warn | 0 | found | warn:found | | missing | non-zero | missing | missing:missing |
exit status
Status `0` means success; non-zero means the command did not succeed.
if command
`if command; then` branches using the command's exit status.
grep -q
`grep -q` searches quietly and reports the result through its status.
Exercise: command_status.sh
Reproduce ok:found, then use the pinned needle variants warn and missing to predict warn:found and missing:missing.