Question 480
A conditional must be true only when two string variables match exactly. Which test fits?
- printf '%s\n' "$value"
- #!/usr/bin/env bash
- [[ $one == "$two" ]]
- set -o pipefail
Explanation
Correct Answer
C:The == operator performs string-pattern comparison in Bash double brackets; quoting the right side forces literal matching. Numeric comparisons use different operators.
Incorrect Answers
A:printf uses an explicit format string and avoids several echo option and escape ambiguities; that changes a different part of the system; the requested outcome is comparing shell strings for exact equality.
B:A #! interpreter header affects how an executable script is launched, not whether two shell variables compare equal. env searches PATH for bash, which can help across distributions; this does not achieve comparing shell strings for exact equality on the system described.
D:pipefail changes a pipeline's exit status to reflect a failing command instead of only its last stage; using it would leave comparing shell strings for exact equality unresolved.
Question 481
A script needs an optional variable default but must not replace an existing nonempty value. Which expansion expresses that?
- $1
- ( VAR=new; command )
- ${PORT:-8080}
- "$@"
- today=$(date +%F)
- ${TOKEN:?TOKEN is required}
Explanation
Correct Answer
C:The :- form supplies a fallback when the variable is unset or null. It does not assign the fallback to the variable.
Incorrect Answers
A:Positional parameters represent arguments after the script name; it does not deliver using a default for an empty or unset parameter, which is decisive in this scenario.
B:Parentheses run shell commands in a subshell context; the evidence calls for using a default for an empty or unset parameter, not merely this separate operation.
D:Quoted $@ preserves each positional argument as a separate shell word; its scope differs from using a default for an empty or unset parameter, so it cannot meet the stated requirement.
E:Command substitution replaces the expression with the command's standard output; that is not equivalent to using a default for an empty or unset parameter.
F:The :? operator reports an error and can exit a noninteractive shell when a required variable is missing; the administrator instead needs using a default for an empty or unset parameter; this option serves a different purpose.
Question 482
A task should iterate over a fixed list of directories without waiting for a condition to change. Which loop is appropriate?
- for dir in /etc /var /opt; do ...; done
- break
- "${items[@]}"
- for ((i=0; i<5; i++)); do ...; done
Explanation
Correct Answer
A:A for loop iterates over supplied words in order. Quoting array-derived values avoids splitting paths with spaces.
Incorrect Answers
B:break is useful for an early exit from an active loop, but it does not describe the regular fixed-list iteration requested. continue would instead skip only the remainder of the current iteration; it answers a neighboring question rather than iterating over a known path list.
C:Quoted array-at expansion yields one word per element while preserving embedded whitespace; that changes a different part of the system; the requested outcome is iterating over a known path list.
D:The arithmetic for loop initializes, tests and increments the counter; this does not achieve iterating over a known path list on the system described.
Question 483
A script should expand all positional arguments as distinct words inside a quoted function call. Which expansion is appropriate?
- today=$(date +%F)
- ${HOST}
- "$@"
- $1
Explanation
Correct Answer
C:Quoted $@ preserves each positional argument as a separate shell word. Quoted $* instead combines them into one string using the IFS separator.
Incorrect Answers
A:Command substitution replaces the expression with the command's standard output; using it would leave preserving argument boundaries while forwarding unresolved.
B:Braced parameter expansion clearly delimits the variable name; the requirement is preserving argument boundaries while forwarding, outside the effect of this choice.
D:Positional parameters represent arguments after the script name; the evidence calls for preserving argument boundaries while forwarding, not merely this separate operation.
Question 484
A systemd service wrapper must replace itself with its child process so signals reach the program directly. Which shell keyword fits?
- exec /usr/bin/worker
- RANDOM
- mktemp
- trap cleanup EXIT
Explanation
Correct Answer
A:exec replaces the shell process with the target command, retaining the PID. It avoids an extra supervising shell layer.
Incorrect Answers
B:Bash's RANDOM produces pseudorandom integers for lightweight shell tasks; that is not equivalent to replacing a wrapper shell with its process.
C:mktemp creates unique temporary files or directories securely using a suitable template; the administrator instead needs replacing a wrapper shell with its process; this option serves a different purpose.
D:A trap for EXIT invokes the named handler when the shell exits under supported conditions; it answers a neighboring question rather than replacing a wrapper shell with its process.
Question 485
A script should run a recovery block only when an earlier command fails. Which pattern expresses this in Bash?
- case "$1" in ... esac
- [[ $a -eq $b ]]
- if ! command; then recover; fi
- [[ $count -gt 100 ]]
Explanation
Correct Answer
C:The ! operator negates the tested command's success status. A branch can act on the failure without relying on textual output.
Incorrect Answers
A:case compares an expression with shell patterns and selects a matching branch; this does not achieve branching on a failed command exit status on the system described.
B:The -eq operator performs an integer numeric comparison in Bash tests; it will not provide branching on a failed command exit status under these conditions.
D:The -gt operator checks strict numeric greater-than; the requirement is branching on a failed command exit status, outside the effect of this choice.
Question 486
A Bash maintenance script should detect unset parameters, propagate failures from early pipeline stages, and exit on many otherwise unhandled failed commands. Which THREE options address these risks? Choose THREE.
- set -u
- printf '%s\n' "$value"
- set -e
- #!/usr/bin/env bash
- [[ $one == "$two" ]]
- set -o pipefail
Explanation
Correct Answers
A:The nounset option reports many references to unset parameters. Exceptions and default expansions need thoughtful handling.
C:Bash set -e (errexit) exits on many otherwise unhandled nonzero statuses, but documented conditional and pipeline contexts are exceptions. It pairs with nounset and pipefail; none eliminates the need to handle expected errors deliberately.
F:set -o pipefail changes pipeline exit-status rules so a failing earlier command is not hidden merely because the last command succeeds. set -e and set -u address different failure classes and are needed separately here.
Incorrect Answers
B:printf uses an explicit format string and avoids several echo option and escape ambiguities; the task involves detecting unset parameters, pipeline errors and unhandled failures, whereas this addresses another subsystem.
D:A shebang can specify Bash as the script interpreter but supplies none of the requested runtime error-checking options. env searches PATH for bash, which can help across distributions; that is not equivalent to detecting unset parameters, pipeline errors and unhandled failures.
E:The == operator performs string-pattern comparison in Bash double brackets; quoting the right side forces literal matching; the administrator instead needs detecting unset parameters, pipeline errors and unhandled failures; this option serves a different purpose.
Question 487
One invalid filename should be ignored while the loop processes the remaining entries. Which command skips only this iteration?
- continue
- while IFS= read -r line; do ...; done < file
- for ((i=0; i<5; i++)); do ...; done
- break
Explanation
Correct Answer
A:continue advances loop control to the next iteration. It does not terminate the script or leave the loop entirely.
Incorrect Answers
B:read -r preserves backslashes, and an empty IFS avoids unintended whitespace trimming; that changes a different part of the system; the requested outcome is skipping a single loop iteration.
C:The arithmetic for loop initializes, tests and increments the counter; this does not achieve skipping a single loop iteration on the system described.
D:break leaves the active loop entirely; continue instead advances to the next iteration when a single entry should be skipped. continue would instead skip only the remainder of the current iteration; it will not provide skipping a single loop iteration under these conditions.
Question 488
Several command-line subcommands require different handlers for start, stop and status. Which Bash construct is well suited?
- if ! command; then recover; fi
- [[ $input =~ ^[a-z0-9]+$ ]]
- [[ -n $token ]]
- case "$1" in ... esac
Explanation
Correct Answer
D:case compares an expression with shell patterns and selects a matching branch. It is often clearer than many repeated tests for discrete commands.
Incorrect Answers
A:The ! operator negates the tested command's success status; the requirement is dispatching across several named subcommands, outside the effect of this choice.
B:The =~ operator tests a Bash regular expression against a string; it does not deliver dispatching across several named subcommands, which is decisive in this scenario.
C:-n succeeds when the expanded string has nonzero length; the evidence calls for dispatching across several named subcommands, not merely this separate operation.
Question 489
A script must print command traces during debugging without altering production business logic. Which shell option can be enabled?
- IFS
- set -x
- trap cleanup EXIT
- mktemp
Explanation
Correct Answer
B:xtrace prints expanded commands before execution, which aids tracing. It can expose secrets in logs and must be disabled in sensitive paths.
Incorrect Answers
A:The Internal Field Separator affects shell word splitting and read parsing; that is not equivalent to tracing shell statements for debugging.
C:A trap for EXIT invokes the named handler when the shell exits under supported conditions; it answers a neighboring question rather than tracing shell statements for debugging.
D:mktemp creates unique temporary files or directories securely using a suitable template; that changes a different part of the system; the requested outcome is tracing shell statements for debugging.
Question 490
A script needs a stable way to print strings that may begin with dashes or contain percent characters. Which builtin is preferable?
- [[ $one == "$two" ]]
- #!/usr/bin/env bash
- printf '%s\n' "$value"
- set -e
Explanation
Correct Answer
C:printf uses an explicit format string and avoids several echo option and escape ambiguities. Quoting preserves the argument's boundaries.
Incorrect Answers
A:The == operator performs string-pattern comparison in Bash double brackets; quoting the right side forces literal matching; it will not provide printing arbitrary string data predictably under these conditions.
B:The interpreter selection header does not provide safe string formatting; Bash printf handles strings predictably. env searches PATH for bash, which can help across distributions; using it would leave printing arbitrary string data predictably unresolved.
D:errexit causes the shell to exit on certain nonzero statuses outside exception contexts; it does not deliver printing arbitrary string data predictably, which is decisive in this scenario.