My cycle is whilerepeate...">

The loop repeats only once with `ssh` in the body

I often see questions that are essentially the same as ...

<h / "> My cycle is whilerepeated only once when I call sshin the body of the cycle.

while read -r line; do
    ssh somehost "command $line"
done < argument_list.txt

+4
source share
1 answer

sshalso reads from standard input, so the first call sshconsumes the remainder argument_list.txtbefore the next call read. To fix, either redirect sshstandard input from /dev/null, using either

ssh somehost "command $line" < /dev/stdin

or

ssh -n somehost "command $line"

, ssh , , argument_list.txt. while.

while read -r line <&3; do
    ssh somehost "command $line"
done 3< argument_list.txt

bash, read -u, , .

while read -r -u 3 line; do
    ssh somehost "command $line"
done 3< argument_list.txt
+5

Source: https://habr.com/ru/post/1542515/


All Articles