Question 460
A script should stop automatically on many unhandled command failures, subject to Bash's exception contexts. Which option is often used?
- #!/usr/bin/env bash
- set -o pipefail
- [[ $one == "$two" ]]
- set -e
- printf '%s\n' "$value"
Explanation
Correct Answer
D:errexit causes the shell to exit on certain nonzero statuses outside exception contexts. It is not a substitute for explicit failure handling.
Incorrect Answers
A:The shebang line chooses a script interpreter at direct execution, but it does not control Bash command failure handling. env searches PATH for bash, which can help across distributions; its scope differs from aborting a script on unhandled failures, so it cannot meet the stated requirement.
B:pipefail changes a pipeline's exit status to reflect a failing command instead of only its last stage; that is not equivalent to aborting a script on unhandled failures.
C:The == operator performs string-pattern comparison in Bash double brackets; quoting the right side forces literal matching; the administrator instead needs aborting a script on unhandled failures; this option serves a different purpose.
E: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 aborting a script on unhandled failures.
Question 461
Two integer counters need comparison for equality, without lexical string interpretation. Which test applies?
- [[ -f /etc/app.conf ]]
- [[ $input =~ ^[a-z0-9]+$ ]]
- [[ -n $token ]]
- [[ $a -eq $b ]]
Explanation
Correct Answer
D:The -eq operator performs an integer numeric comparison in Bash tests. String equality uses operators such as == instead.
Incorrect Answers
A:-f evaluates true for an existing regular file, with symlink dereference under normal shell test behavior; this does not achieve comparing numbers for equality on the system described.
B:The =~ operator tests a Bash regular expression against a string; it will not provide comparing numbers for equality under these conditions.
C:-n succeeds when the expanded string has nonzero length; using it would leave comparing numbers for equality unresolved.
Question 462
A retry routine should run until a health-check command begins to succeed. Which Bash loop naturally expresses this?
- continue
- for ((i=0; i<5; i++)); do ...; done
- "${items[@]}"
- until healthcheck; do sleep 2; done
Explanation
Correct Answer
D:An until loop repeats while its test returns a failure status. An upper retry limit should be added for robust automation.
Incorrect Answers
A:continue advances loop control to the next iteration; the evidence calls for retrying while a health check fails, not merely this separate operation.
B:The arithmetic for loop initializes, tests and increments the counter; the task involves retrying while a health check fails, whereas this addresses another subsystem.
C:Quoted array-at expansion yields one word per element while preserving embedded whitespace; its scope differs from retrying while a health check fails, so it cannot meet the stated requirement.
Question 463
A Bash script needs the value of variable HOST inside a string without confusing adjacent characters with the variable name. Which syntax is suitable?
- ( VAR=new; command )
- ${PORT:-8080}
- ${HOST}
- "$@"
Explanation
Correct Answer
C:Braced parameter expansion clearly delimits the variable name. It is especially useful when immediately followed by other characters.
Incorrect Answers
A:Parentheses run shell commands in a subshell context; it answers a neighboring question rather than delimiting a variable name during expansion.
B:The :- form supplies a fallback when the variable is unset or null; that changes a different part of the system; the requested outcome is delimiting a variable name during expansion.
D:Quoted $@ preserves each positional argument as a separate shell word; it will not provide delimiting a variable name during expansion under these conditions.
Question 464
A loop should stop as soon as an expected record is found. Which keyword exits the innermost loop?
- for ((i=0; i<5; i++)); do ...; done
- "${items[@]}"
- break
- for dir in /etc /var /opt; do ...; done
Explanation
Correct Answer
C:In Bash, break immediately terminates the innermost enclosing loop, skipping its remaining iterations. continue would instead skip only the remainder of the current iteration.
Incorrect Answers
A:The arithmetic for loop initializes, tests and increments the counter; the requirement is terminating a loop before normal completion, outside the effect of this choice.
B:Quoted array-at expansion yields one word per element while preserving embedded whitespace; it does not deliver terminating a loop before normal completion, which is decisive in this scenario.
D:A for loop iterates over supplied words in order; the task involves terminating a loop before normal completion, whereas this addresses another subsystem.
Question 465
A pipeline should report failure when an earlier command fails even if the last command succeeds. Which Bash option helps?
- [[ $one == "$two" ]]
- set -e
- printf '%s\n' "$value"
- set -o pipefail
Explanation
Correct Answer
D:pipefail changes a pipeline's exit status to reflect a failing command instead of only its last stage. It helps automation catch hidden processing errors.
Incorrect Answers
A:The == operator performs string-pattern comparison in Bash double brackets; quoting the right side forces literal matching; that is not equivalent to detecting failures in earlier pipeline stages.
B:errexit causes the shell to exit on certain nonzero statuses outside exception contexts; the administrator instead needs detecting failures in earlier pipeline stages; this option serves a different purpose.
C:printf uses an explicit format string and avoids several echo option and escape ambiguities; it answers a neighboring question rather than detecting failures in earlier pipeline stages.
Question 466
A temporary variable assignment in a subshell must not change the parent shell's value. Which grouping form creates that boundary?
- ${TOKEN:?TOKEN is required}
- today=$(date +%F)
- "$@"
- ( VAR=new; command )
- $1
Explanation
Correct Answer
D:Parentheses run shell commands in a subshell context. Variable changes made there do not persist in the caller shell.
Incorrect Answers
A:The :? operator reports an error and can exit a noninteractive shell when a required variable is missing; it will not provide isolating shell-variable mutations to a subshell under these conditions.
B:Command substitution replaces the expression with the command's standard output; using it would leave isolating shell-variable mutations to a subshell unresolved.
C:Quoted $@ preserves each positional argument as a separate shell word; the requirement is isolating shell-variable mutations to a subshell, outside the effect of this choice.
E:Positional parameters represent arguments after the script name; the evidence calls for isolating shell-variable mutations to a subshell, not merely this separate operation.
Question 467
A Bash script needs the first positional parameter passed at invocation time. Which variable refers to it?
- $1
- $?
- ( VAR=new; command )
- ${TOKEN:?TOKEN is required}
Explanation
Correct Answer
A:Positional parameters represent arguments after the script name. Quoting is important when arguments contain whitespace.
Incorrect Answers
B:$? expands to the most recent command or pipeline's exit status under normal shell semantics; its scope differs from reading the first shell-script argument, so it cannot meet the stated requirement.
C:Parentheses run shell commands in a subshell context; that is not equivalent to reading the first shell-script argument.
D:The :? operator reports an error and can exit a noninteractive shell when a required variable is missing; the administrator instead needs reading the first shell-script argument; this option serves a different purpose.
Question 468
A list of whitespace-separated input tokens should use a custom separator while read processes records. Which shell variable controls field splitting?
- exec /usr/bin/worker
- command >combined.log 2>&1
- set -x
- RANDOM
- mktemp
- IFS
Explanation
Correct Answer
F:The Internal Field Separator affects shell word splitting and read parsing. It must be changed carefully so filenames or spaces are not unintentionally split.
Incorrect Answers
A:exec replaces the shell process with the target command, retaining the PID; that changes a different part of the system; the requested outcome is controlling shell field splitting.
B:Redirecting stdout first and then duplicating it for stderr directs both streams to one file; this does not achieve controlling shell field splitting on the system described.
C:xtrace prints expanded commands before execution, which aids tracing; it will not provide controlling shell field splitting under these conditions.
D:Bash's RANDOM produces pseudorandom integers for lightweight shell tasks; using it would leave controlling shell field splitting unresolved.
E:mktemp creates unique temporary files or directories securely using a suitable template; the requirement is controlling shell field splitting, outside the effect of this choice.
Question 469
An administrator needs to output both stdout and stderr from a command to a single file in the intended order. Which redirection is appropriate?
- exec /usr/bin/worker
- set -x
- IFS
- command >combined.log 2>&1
Explanation
Correct Answer
D:Redirecting stdout first and then duplicating it for stderr directs both streams to one file. Reversing the redirections can produce different results.
Incorrect Answers
A:exec replaces the shell process with the target command, retaining the PID; it does not deliver merging stdout and stderr into one file, which is decisive in this scenario.
B:xtrace prints expanded commands before execution, which aids tracing; the evidence calls for merging stdout and stderr into one file, not merely this separate operation.
C:The Internal Field Separator affects shell word splitting and read parsing; the task involves merging stdout and stderr into one file, whereas this addresses another subsystem.
Question 470
A shell function should process array elements containing spaces as distinct values. Which expansion is safe?
- "${items[@]}"
- for dir in /etc /var /opt; do ...; done
- for ((i=0; i<5; i++)); do ...; done
- until healthcheck; do sleep 2; done
Explanation
Correct Answer
A:Quoted array-at expansion yields one word per element while preserving embedded whitespace. Unquoted expansion can trigger splitting and globbing.
Incorrect Answers
B:A for loop iterates over supplied words in order; it answers a neighboring question rather than preserving array element boundaries in iteration.
C:The arithmetic for loop initializes, tests and increments the counter; that changes a different part of the system; the requested outcome is preserving array element boundaries in iteration.
D:An until loop repeats while its test returns a failure status; this does not achieve preserving array element boundaries in iteration on the system described.