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.
Question 471
The shell must test whether /etc/app.conf exists as a regular file. Which test operator fits?
- case "$1" in ... esac
- [[ $a -eq $b ]]
- [[ -f /etc/app.conf ]]
- if ! command; then recover; fi
Explanation
Correct Answer
C:-f evaluates true for an existing regular file, with symlink dereference under normal shell test behavior. It does not merely test directory existence.
Incorrect Answers
A:case compares an expression with shell patterns and selects a matching branch; using it would leave testing for a regular file unresolved.
B:The -eq operator performs an integer numeric comparison in Bash tests; the requirement is testing for a regular file, outside the effect of this choice.
D:The ! operator negates the tested command's success status; the evidence calls for testing for a regular file, not merely this separate operation.
Question 472
A script should continue reading lines from a text file until EOF. Which loop idiom supports this?
- for dir in /etc /var /opt; do ...; done
- until healthcheck; do sleep 2; done
- while IFS= read -r line; do ...; done < file
- "${items[@]}"
Explanation
Correct Answer
C:read -r preserves backslashes, and an empty IFS avoids unintended whitespace trimming. Redirection feeds the file to the loop.
Incorrect Answers
A:A for loop iterates over supplied words in order; its scope differs from reading a file safely line by line, so it cannot meet the stated requirement.
B:An until loop repeats while its test returns a failure status; that is not equivalent to reading a file safely line by line.
D:Quoted array-at expansion yields one word per element while preserving embedded whitespace; it answers a neighboring question rather than reading a file safely line by line.
Question 473
An input should be accepted only if it matches a Bash regular expression anchored at the start and end. Which operator is suitable?
- if ! command; then recover; fi
- [[ -f /etc/app.conf ]]
- [[ $count -gt 100 ]]
- [[ $input =~ ^[a-z0-9]+$ ]]
- [[ -n $token ]]
Explanation
Correct Answer
D:The =~ operator tests a Bash regular expression against a string. Anchors constrain the full input rather than just a substring.
Incorrect Answers
A:The ! operator negates the tested command's success status; this does not achieve validating an entire string against a regex on the system described.
B:-f evaluates true for an existing regular file, with symlink dereference under normal shell test behavior; it will not provide validating an entire string against a regex under these conditions.
C:The -gt operator checks strict numeric greater-than; using it would leave validating an entire string against a regex unresolved.
E:-n succeeds when the expanded string has nonzero length; it does not deliver validating an entire string against a regex, which is decisive in this scenario.
Question 474
A new script must explicitly select Bash when invoked as an executable on Unix-like systems. Which first line applies?
- set -e
- set -u
- #!/usr/bin/env bash
- [[ $one == "$two" ]]
Explanation
Correct Answer
C:A first-line #! sequence tells the kernel which interpreter to launch when a script file is executed as a program. env searches PATH for bash, which can help across distributions.
Incorrect Answers
A:errexit causes the shell to exit on certain nonzero statuses outside exception contexts; the evidence calls for selecting a Bash interpreter through a shebang, not merely this separate operation.
B:The nounset option reports many references to unset parameters; the task involves selecting a Bash interpreter through a shebang, whereas this addresses another subsystem.
D:The == operator performs string-pattern comparison in Bash double brackets; quoting the right side forces literal matching; that is not equivalent to selecting a Bash interpreter through a shebang.
Question 475
A nonempty token is required before an API call. Which condition tests that a variable has content?
- if ! command; then recover; fi
- [[ -n $token ]]
- case "$1" in ... esac
- [[ $input =~ ^[a-z0-9]+$ ]]
Explanation
Correct Answer
B:-n succeeds when the expanded string has nonzero length. Testing it avoids calling an API with an empty credential.
Incorrect Answers
A:The ! operator negates the tested command's success status; it answers a neighboring question rather than checking that a string is nonempty.
C:case compares an expression with shell patterns and selects a matching branch; this does not achieve checking that a string is nonempty on the system described.
D:The =~ operator tests a Bash regular expression against a string; it will not provide checking that a string is nonempty under these conditions.
Question 476
A script should test whether /srv/reports exists as a directory. Which Bash test is appropriate?
- [[ -f /etc/app.conf ]]
- [[ -n $token ]]
- case "$1" in ... esac
- [[ -d /srv/reports ]]
Explanation
Correct Answer
D:The -d file-test operator succeeds when the path refers to an existing directory, including one reached through a valid symlink. It says nothing about directory emptiness.
Incorrect Answers
A:-f evaluates true for an existing regular file, with symlink dereference under normal shell test behavior; the requirement is checking directory existence, outside the effect of this choice.
B:-n succeeds when the expanded string has nonzero length; it does not deliver checking directory existence, which is decisive in this scenario.
C:case compares an expression with shell patterns and selects a matching branch; the evidence calls for checking directory existence, not merely this separate operation.
Question 477
A script must inspect the exit status of the immediately preceding foreground command. Which parameter contains it?
- ${TOKEN:?TOKEN is required}
- $?
- $1
- ${PORT:-8080}
Explanation
Correct Answer
B:$? expands to the most recent command or pipeline's exit status under normal shell semantics. It should be captured before running another command.
Incorrect Answers
A:The :? operator reports an error and can exit a noninteractive shell when a required variable is missing; that is not equivalent to reading the previous command's exit status.
C:Positional parameters represent arguments after the script name; it answers a neighboring question rather than reading the previous command's exit status.
D:The :- form supplies a fallback when the variable is unset or null; that changes a different part of the system; the requested outcome is reading the previous command's exit status.
Question 478
A script must create temporary files without guessing a collision-prone filename. Which command is designed for this?
- trap cleanup EXIT
- mktemp
- set -x
- exec /usr/bin/worker
Explanation
Correct Answer
B:mktemp creates unique temporary files or directories securely using a suitable template. Cleanup should be arranged through an exit trap.
Incorrect Answers
A:A trap for EXIT invokes the named handler when the shell exits under supported conditions; it will not provide allocating unpredictable temporary paths under these conditions.
C:xtrace prints expanded commands before execution, which aids tracing; the requirement is allocating unpredictable temporary paths, outside the effect of this choice.
D:exec replaces the shell process with the target command, retaining the PID; it does not deliver allocating unpredictable temporary paths, which is decisive in this scenario.
Question 479
A Bash function needs to return a success or failure status to its caller rather than print a value. Which statement sets it?
- set -o pipefail
- return 1
- [[ $one == "$two" ]]
- #!/usr/bin/env bash
- set -e
Explanation
Correct Answer
B:return sets a shell function's numeric exit status, with nonzero normally indicating failure. It does not return an arbitrary string object like Python.
Incorrect Answers
A:pipefail changes a pipeline's exit status to reflect a failing command instead of only its last stage; the task involves returning a function's failure status, whereas this addresses another subsystem.
C:The == operator performs string-pattern comparison in Bash double brackets; quoting the right side forces literal matching; that is not equivalent to returning a function's failure status.
D:The shebang selects a program to interpret the script; a function's return status is a separate Bash control-flow feature. env searches PATH for bash, which can help across distributions; the administrator instead needs returning a function's failure status; this option serves a different purpose.
E:errexit causes the shell to exit on certain nonzero statuses outside exception contexts; it answers a neighboring question rather than returning a function's failure status.
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.