Interview Prep/Shell Scripting/Troubleshooting: quoting, expansion and tool-input bugs

A script that reads `hosts.txt` and SSHes to each host only processes the first host, with no error.

IntermediateTroubleshootingTroubleshooting: quoting, expansion and tool-input bugs
Answer

Likely causes → ssh inherits the loop's stdin and reads the rest of hosts.txt as input for the remote command, so the loop ends after one iteration. docker exec -i, kubectl exec -i and mysql do the same.

Diagnose → Add echo "next: $host" at the top of the loop; it prints once.

Fix → ssh -n "$host" ... (stdin from /dev/null), ssh ... < /dev/null, or read the list on another descriptor: while read -r host <&3; do ...; done 3< hosts.txt.

Prevent → Always use ssh -n (or -o BatchMode=yes plus < /dev/null) inside read loops.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: quoting, expansion and tool-input bugs