环只与`ssh`迭代一次身体 [英] Loop only iterates once with `ssh` in the body

查看:83
本文介绍了环只与`ssh`迭代一次身体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常看到它在本质上是相同的问题...



我的,而循环迭代只有一次,当我打电话 SSH 中的循环体。

 同时读取-r线;做
    SSH,某命令行$
完成< argument_list.txt



解决方案

SSH 也从标准输入读取,所以第一次调用 SSH 下一次调用之前消耗 argument_list.txt 的其余部分。要解决,无论是重定向的标准输入从的/ dev / null的使用 SSH 或者

  SSH,某命令行$<为/ dev /标准输入

  SSH -n,某命令行$

在事件 SSH 真正的的需要从标准输入读取数据,你不希望它从<$ C读取更多数据$ C> argument_list.txt 。在这种情况下,你需要使用不同的文件描述符while循环。

 同时读取-r线474;及3;做
    SSH,某命令行$
做3'; argument_list.txt

庆典和其他一些炮弹也让<$ ​​C $ C>读取 -u 选项指定的文件描述符,其中一些可能会发现更容易阅读。

 同时读取-r -u 3线;做
    SSH,某命令行$
做3'; argument_list.txt

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


My while loop only iterates once when I call ssh in the body of the loop.

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


解决方案

ssh also reads from standard input, so the first call to ssh consumes the rest of argument_list.txt before the next call to read. To fix, either redirect ssh's standard input from /dev/null using either

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

or

ssh -n somehost "command $line"

In the event that ssh really does need to read from standard input, you don't want it reading more data from argument_list.txt. In this case, you need to use a different file descriptor for the while loop.

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

bash and some other shells also allow read to take a -u option to specify the file descriptor, which some might find more readable.

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

这篇关于环只与`ssh`迭代一次身体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆