在linux的kshell脚本中需要帮助for循环

时间:2017-07-01 14:53:31

标签: linux shell scripting ksh

有人能说出以下内容如何运作?

server.ksh file contains the below.
n=$1   ### what does this do ? 
ssh $n date;

test是一个包含50个服务器列表的文件。 现在我运行下面的内容。

for a in `cat test`; do ksh server.ksh $a >> date$a & done

只要随意查看上面你可以说它只是打印所有服务器的日期并保存o / p。但问题是它在同一时间点击所有服务器!! < / strong>(即不像传统的for循环一个接一个)。有人可以打破这个并解释一下吗?

1 个答案:

答案 0 :(得分:0)

# assign variable 'n' the value of the first input parameter to
# the server.ksh invocation; a server name in this case
n=$1

# make a ssh call to the server whose name is stored in
# variable 'n', on the remote host run the 'date' command
ssh $n date;

您的while循环正在后台启动server.ksh来电(&amp; );这允许循环继续到下一次迭代,同时仍在处理server.ksh调用;最终结果是,您最终会同时在线投放多个ssh来电。

它看起来只是在同一时间完成所有事情,因为date命令的默认输出仅精确到最接近的秒。如果您提高date输出的精确度,您可能会看到ssh调用未在“完全”同时完成,例如:

# output format: HH:MM:SS.nnnnnnnnn (nano-seconds)
ssh $n "date +'%H:%M:%S.%N'";

显然(?)如果各种服务器的系统时钟关闭甚至只有几分之一秒,那么date输出也可能存在差异。