bash – 循环只在主体中使用`ssh`迭代一次
发布时间:2020-12-15 21:10:32 所属栏目:安全 来源:网络整理
导读:参见英文答案 ssh breaks out of while-loop in bash ????????????????????????????????????2个 我经常看到与……基本相同的问题 当我在循环体中调用ssh时,我的while循环只迭代一次. while read -r line; do ssh somehost "command $line"done argument_list
参见英文答案 >
ssh breaks out of while-loop in bash ????????????????????????????????????2个
我经常看到与……基本相同的问题 当我在循环体中调用ssh时,我的while循环只迭代一次. while read -r line; do ssh somehost "command $line" done < argument_list.txt 解决方法
ssh也从标准输入读取,因此第一次调用ssh会在下一次read调用之前使用其余的argument_list.txt.要修复,请使用其中任何一个重定向ssh的标准输入/ dev / null
ssh somehost "command $line" < /dev/stdin 要么 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和其他一些shell也允许read使用-u选项来指定文件描述符,有些人可能会发现它更具可读性. while read -r -u 3 line; do ssh somehost "command $line" done 3< argument_list.txt (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |