A script must process /etc/hosts one whole line at a time, including lines that contain spaces. Which loop does that correctly?
LPIC-1 Exam 102-500, objective 105. Shells and shell scripting medium
Machine-checked — no person has signed for it. This question was read against the source cited below by an automated pass, which found no contradiction. That is a weaker claim than it sounds: the same kind of process wrote the question, so it can confirm its own mistake.
Treat it as a good draft rather than as settled fact, and read the source below before you rely on it. It is not used in mock exams here — only questions a person has signed for are.
How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.
The options
Correct while read -r line; do process "$line"; done < /etc/hosts
Correct. read consumes one line per iteration and returns non-zero at end of file, which ends the loop. The redirection is attached to the whole loop, so the file is opened once.
Not correct for line in $(cat /etc/hosts); do process "$line"; done
Wrong. The command substitution is subject to word splitting on IFS, so the loop iterates once per whitespace-separated word, not once per line, and it also glob-expands anything that looks like a pattern.
Not correct while read -r line < /etc/hosts; do process "$line"; done
Wrong, and it never terminates. The redirection is on the read itself, so the file is reopened from the start on every iteration and read keeps returning the first line.
Not correct until read -r line; do process "$line"; done < /etc/hosts
Wrong, and inverted. until runs its body while the condition command fails. read succeeds on the first line, so the body never runs at all.
Why
while read is the line-oriented idiom because read returns a zero exit status for every line it delivers and non-zero at end of input, which is exactly the loop condition needed. Attach the redirection to done rather than to read, or the file is reopened each pass. -r stops read from treating backslashes as escapes, and setting IFS= in front of read preserves leading and trailing whitespace.
Where this comes from
- Cited
- manual page bash(1)
Practise this
Reading one question is not practice. The trainer will draw a short set from objective 105 and space the ones you get wrong.
More questions on this objective
- A user logs in at a text console, and bash starts as an interactive login shell. Which file in that user's home directory is read by bash itself as part of login startup? machine-checked
- You are already logged in to a graphical desktop and open a new terminal window, so bash starts as an interactive shell that is not a login shell. Which per-user file does bash read in that case? machine-checked
- An executable script contains only the line `export EDITOR=vim`. After running it as `./setenv.sh`, `echo $EDITOR` in the calling shell prints nothing. What explains this? machine-checked
- In bash, what is the effect of `export MYVAR`? machine-checked
- At a bash prompt you run `alias ll='ls -l'` and it works for the rest of that session, but a terminal window opened afterwards does not recognise `ll`. Which statement explains this and gives the standard remedy? machine-checked
- A command finishes and the shell reports an exit status of 0. What does that mean, and which parameter reports it? machine-checked