Courseiva
Create simple shell scriptseasyMultiple ChoiceObjective-mapped

EX200 Create simple shell scripts Practice Question

A system administrator needs to create a shell script that processes a list of hostnames stored in a file, one per line, and runs a command on each host. Which loop construct is most appropriate?

⚠ Common exam trap

Candidates often choose `for host in $(cat hosts)` because it looks simpler, but they overlook how word splitting and glob expansion can break the script when hostnames contain spaces, tabs, or wildcard characters.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

while read host; do ... done < hosts

The `while read host; do ... done < hosts` construct reads the file line by line, preserving each hostname exactly as it appears, including spaces or special characters. This is the safest and most idiomatic way to process a list of hostnames in a shell script, as it avoids word splitting and glob expansion issues that can occur with other methods.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • while read host; do ... done < hosts

    Why this is correct

    The `while read host; do ... done < hosts` construct is the correct approach because it reads the file line by line. On each iteration, `read` assigns the entire line (minus trailing newline) to the variable `host`, so the content is not subjected to word splitting or pathname expansion. This makes it robust for hostnames that might contain unusual characters, though for exact preservation one should add `IFS=` and `-r` to `read`. Additionally, the redirection `< hosts` attaches the file to the loop's standard input, and the loop runs in the current shell, so any variable updates inside the loop remain available afterward.

  • for i in $(seq 1 $(wc -l < hosts)); do ... done

    Why it's wrong here

    Incorrect. This approach uses a for loop that generates a sequence of line numbers using seq and wc, then reads lines with sed. It is unnecessarily complex and fragile; any change in whitespace or special characters in hostnames can cause issues. Additionally, it spawns multiple subshells and is inefficient.

  • for host in $(cat hosts); do ... done

    Why it's wrong here

    Incorrect. Using `for host in $(cat hosts); do ... done` subjects the content to word splitting and glob expansion. If a hostname contains spaces or wildcard characters, it will be split or expanded, leading to errors.

  • until read host; do ... done < hosts

    Why it's wrong here

    Incorrect. The `until` loop is the inverse of `while`; it continues until the condition is true. The condition `read host` returns true as long as lines are read, so this loop would not execute at all because `until read host` fails immediately (read returns 0 when a line is read, so condition is false at start). Thus it never reads any lines.

About these practice questions

This EX200 question is part of Courseiva's 127-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This EX200 practice question is part of Courseiva's free Red Hat certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the EX200 exam.