The eventual goal is to have my bash script execute a command on multiple servers. I almost have it set up. My SSH authentication is working, but this simple while loop is
If you run commands which read from stdin (such as ssh
) inside a loop, you need to ensure that either:
...otherwise, the command can consume input intended for the loop, causing it to end.
The former:
while read -u 5 -r hostname; do
ssh "$hostname" ...
done 5
...which, using bash 4.1 or newer, can be rewritten with automatic file descriptor assignment as so:
while read -u "$file_fd" -r hostname; do
ssh "$hostname" ...
done {file_fd}
The latter:
while read -r hostname; do
ssh "$hostname" ...
...can also, for ssh alone, can also be approximated with the -n
parameter (which also redirects stdin from /dev/null
):
while read -r hostname; do
ssh -n "$hostname"
done