Sample questions
Linux Shell Scripting Ops AutomationDifficulty 1
A deploy script starts with #!/usr/bin/env bash followed by set -e on the next line. What does set -e do?
- aIt makes the script exit immediately if any simple command returns a non-zero exit status.✓
- bIt enables verbose mode, printing every command to stderr before it runs
- cIt tells bash to treat unset variables as an error and exit
- dIt exports every variable assigned afterward into the environment automatically
Explanation:set -e (errexit) causes the shell to stop executing the script as soon as a command exits with a non-zero status, which is exactly the fail-fast behavior an ops script wants instead of silently continuing after a failed step. Option c describes set -u, option b describes set -x, and option d describes set -a/export.
Linux Shell Scripting Ops AutomationDifficulty 1
A health-check script references $SERVICE_NAME but the variable was never set because of a typo (SERVICE_NAM=web earlier in the script). With set -u enabled, what happens when the script reaches the reference to $SERVICE_NAME?
- aBash silently substitutes an empty string and continues running the rest of the script
- bThe shell treats the reference to an unset variable as an error and the script exits with a non-zero status✓
- cBash automatically falls back to the last variable that was assigned in the script
- d
set -u only affects variables used inside functions, so the script continues normally at the top level
Explanation:set -u (nounset) makes referencing an unset variable an error that terminates the script (when combined with -e, or on its own for that command), which is exactly why it's useful for catching typos like SERVICE_NAM vs SERVICE_NAME early instead of the script silently working with an empty value.
Linux Shell Scripting Ops AutomationDifficulty 2
A log-parsing script runs cat access.log | grep ERROR | wc -l to count error lines. Why would adding set -o pipefail at the top of the script matter for this specific line?
- aIt makes
wc -l count bytes instead of lines, giving a more precise error count - bIt forces
grep to run before cat, reversing the pipeline's execution order - cWithout it, the pipeline's exit status is only that of the last command, so an earlier failure in
cat or grep would be silently hidden.✓ - dIt automatically retries the whole pipeline up to three times if any stage fails
Explanation:By default, a pipeline's exit status is the exit status of its last command, so if cat access.log failed (e.g., file missing) but grep/wc -l still ran and produced some output, the failure would go unnoticed. set -o pipefail makes the pipeline's exit status reflect the last command that actually failed, letting set -e (or manual $? checks) catch it.
Linux Shell Scripting Ops AutomationDifficulty 1
In a shell script, what does an exit code of 0 conventionally mean for the command or script that just finished?
- aThe command is still running in the background and has not finished yet
- bThe command produced no output at all, regardless of whether it succeeded
- cThe command was skipped entirely because of a prior
set -e failure - dThe command completed successfully, with no error✓
Explanation:By long-standing Unix convention, an exit status of 0 means success, and any non-zero value (1-255) signals some kind of failure or specific error condition. Scripts and pipelines rely on this convention to decide whether to continue, retry, or abort.
Linux Shell Scripting Ops AutomationDifficulty 1
A deploy script has this line:
mkdir -p /opt/app/releases/current && systemctl restart app
What does the
&& between the two commands guarantee?
- aBoth commands always run, and
&& only affects how their output is displayed - b
systemctl restart app only runs if mkdir -p ... exits with status 0 (succeeds)✓ - cThe two commands run at the same time, in parallel background jobs
- d
systemctl restart app runs first, and mkdir -p ... runs only if it fails
Explanation:&& is a conditional AND: the command on the right only executes if the command on the left exited successfully (status 0). Here that means the restart is only attempted after the directory was actually created, avoiding restarting a service into a half-prepared release directory.
Linux Shell Scripting Ops AutomationDifficulty 2
A script contains:
ping -c 1 db.internal || echo "WARNING: db.internal unreachable" >> health.log
When does the
echo command run?
- aEvery time the script runs, regardless of whether
ping succeeds or fails - bOnly if
ping succeeds, since || requires the left side to succeed first - cOnly if
ping -c 1 db.internal exits with a non-zero status (fails)✓ - dNever, because
|| is only valid between two echo commands
Explanation:|| is a conditional OR: the right-hand command only runs if the left-hand command failed (non-zero exit). So the warning is appended to the log only when the ping fails, which is the typical pattern for a lightweight fallback/alerting action in a health-check script.