Avoiding input problems with ssh

Another vexing problem with reading lines in a while read -r loop is that sometimes certain commands inside the loop can themselves consume some of the data intended for the read builtin, and it may not be immediately clear what those commands are.

One such command is the ssh OpenSSH client, which by default reads all the standard input it can once started to pass it as commands to the target system, even if a command is specified on the command line. Consider this list of SSH hostnames:

$ cat hostnames
alpha.example.com.
beta.example.com.
gamma.example.com.

We want to read each hostname, and run the uptime command on it to get a quick overview of how long it's been up and its load average. However, this doesn't ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.