为什么SSH打破了Bash的控制流 [英] Why SSH breaks the control flow in Bash

查看:101
本文介绍了为什么SSH打破了Bash的控制流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过文件下面的脚本扫描,每一行是一个远程节点的主机名。

I have following script scanning through a file, each line is hostname of a remote node.

echo -e "node1\nnode2\nnode3" > tempfile
while read aline; do
  echo "@$aline";  
done < tempfile

这将产生 @节点1 @节点2 @节点3 正确三行。但是,当我加入 SSH 内循环,如下:

this produces @node1 @node2 and @node3 correctly in three lines. But when I add ssh inside the loop, as follows

while read aline; do
  echo "@$aline"; 
  ssh $aline 'jps';  
done < tempfile

SSH 的第一次调用,并打印后的循环将打破只有 @节点1 (不包括 @节点2 `@节点3 )。

The loop will break after first invocation of ssh and prints only @node1 (without @node2 and `@node3).

我问发生了什么事的场景(它看起来像未定义行为)的后面?一应如何实现相同的功能,而不打破while循环。

I am asking what happened behind the scene (it looks like undefined behaviour)? And how should one realise the same functionality without breaking the while loop.

推荐答案

SSH是干什么用标准输入的东西(这是从临时文件重定向)和搞乱了秒。尝试重定向标准输入。

SSH is doing something with stdin (which is redirected from tempfile) and messing up the reads. Try redirecting stdin.

ssh -n $aline 'jps'

男人SSH

-n 从/ dev / null的(实际上,prevents从标准输入读取)。

-n Redirects stdin from /dev/null (actually, prevents reading from stdin).

这篇关于为什么SSH打破了Bash的控制流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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