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
#!/usr/bin/env bash
data=$'ok\nwarn\nfail'
needle=
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=
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=
if printf '%s\n' "$data" | grep -q "$needle"; then
label="found"
else
label="missing"
fi
echo "$needle:$label"
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.