如何保持脚本吞咽所有的标准input?

我有一个脚本从一个循环pipe道中读取,并在循环中运行期望脚本和正常的shell脚本。 这两个脚本运行ssh到另一台服务器来获取一块数据。 例如:

cat /tmp/file | while read abcd do s=`expect-script server1 $b` c=`ssh $b normal-script` echo $s $c done 

即使/ tmp / file中有很多行,脚本在处理第一行之后仍然退出。 我怀疑期望的脚本是吞咽所有的标准input,所以当它返回时,没有什么可读的。 我怎样才能避免这一点? 我不希望任何我调用的脚本从主脚本的stdin中读取。

 cat /tmp/file | while read abcd do { s=`expect-script server1 $b` c=`ssh $b normal-script` echo $s $c } < /dev/null done 

{ command... }语法允许您将redirect或pipe道应用于一系列命令。

我也会注意到你的例子中你不需要cat 。 你可以这样做:

 while read abcd do ... done < /tmp/file 

这实际上是ssh ,嘿嘿stdin。 只需添加-n选项:

  c=$( ssh -n $b normal-script ) 

如果你不想这样做,你可以让你的shell while循环从不同的文件描述符中读取, while不用改变stdin。

 while read -u3 abcd do s=$( expect-script server1 $b ) c=$( ssh $b normal-script ) echo $s $c done 3< /tmp/file 

(假设read -u选项为bash / ksh / zsh)