Shell Scripting & Automation Flashcards
7 cards from real LCA practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Shell Scripting & Automation flashcards as text
Which construct is used in bash to iterate over a list of files matching a glob pattern?
Answer: for f in /etc/*.conf; do ...; done
The for loop with a glob pattern expands the pattern and iterates over each matching file.
What does the `set -e` option do in a bash script?
Answer: Exits the script immediately if any command returns a non-zero exit status
`set -e` causes the shell to exit immediately when a command exits with a non-zero status.
Which command reads a line of input from the user and stores it in a variable named REPLY?
Answer: read
The `read` command without a variable name stores input in the default variable $REPLY.
In a bash script, what is the value of `$?` after a command succeeds?
Answer: 0
A zero exit status ($?) indicates that the previous command completed successfully.
What does `trap 'rm -f /tmp/lockfile' EXIT` accomplish in a script?
Answer: Removes the lockfile when the script exits for any reason
The `trap ... EXIT` command registers a cleanup handler that runs whenever the script exits, including on error or signal.
Which bash feature allows you to test if a variable is set and non-empty using `[[ -n $VAR ]]`?
Answer: String length test
The `-n` flag inside `[[ ]]` tests whether the string length is non-zero, i.e., the variable is set and non-empty.
What output does `echo ${#MYVAR}` produce if MYVAR='hello'?
Answer: 5
The `${#var}` parameter expansion returns the length of the string stored in the variable.