Interview Prep/Shell Scripting/Arrays, conditionals and loops

Why does an `ssh` or `docker exec` command inside a `while read` loop only process the first line?

AdvancedConceptArrays, conditionals and loops
Answer

The loop reads from stdin, and ssh (also docker exec -i, ffmpeg, mysql) also reads stdin, consuming the rest of the file in the first iteration.

while IFS= read -r host; do
  ssh -n "$host" uptime              # -n: stdin from /dev/null
done < hosts.txt

# or give the loop its own file descriptor
while IFS= read -r host <&3; do
  ssh "$host" uptime
done 3< hosts.txt
All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Arrays, conditionals and loops