gpt4 book ai didi

linux - 需要 Linux shell 脚本中 for 循环的帮助

转载 作者:太空宇宙 更新时间:2023-11-04 05:18:54 24 4
gpt4 key购买 nike

有人能说一下下面的工作原理吗?

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。但问题是它在同一时间到达所有服务器!!(即不像传统的 for 循环一个接一个)。有人可以打破这个并解释一下吗?

最佳答案

# 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 调用 (&);这允许循环继续到下一次迭代,同时 server.ksh 调用仍在处理中;最终结果是您最终会同时进行多个 ssh 调用。

看起来一切都是在同一时间完成的,因为 date 命令的默认输出仅精确到最接近的秒。如果您提高 date 输出的精度,您可能会发现 ssh 调用并非“完全”同时完成,例如:

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

显然(?)如果各个服务器的系统时钟有哪怕零点几秒的偏差,日期输出也可能存在差异。

关于linux - 需要 Linux shell 脚本中 for 循环的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44862281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com