gpt4 book ai didi

shell - 如何创建一个脚本来依次执行多个 "exec"命令?

转载 作者:行者123 更新时间:2023-12-01 06:58:06 27 4
gpt4 key购买 nike

我对 Linux Shell 脚本很陌生,想知道是否有人可以帮助我解决以下问题。

我创建了一个脚本来与我的 linux 机器同步时间,但似乎只有一个 exec 命令完成

#!/bin/bash
#Director SMS Synch Time Script

echo The current date and time is:
date
echo

echo Synching GTS Cluster 1 directors with SMS.
echo
echo Changing date and time for director-1-1-A
exec ssh root@128.221.252.35 "ntpd -q -g"
echo Finished synching director-1-1-A
echo

sleep 2

echo Changing date and time for director-1-1-B
exec ssh root@128.221.252.36 "ntp -q -g"
echo Finished synching director-1-1-B
echo

sleep 2

echo Finished Synching GTS Cluster 1 directors with SMS.
sleep 2
echo
echo Synching SVT Cluster 2 directors with SMS.
echo
echo Changing date and time for director-2-1-A
exec ssh root@128.221.252.67 "ntpd -q -g"
echo Finished synching director-2-1-A
echo

sleep 2

echo Changing date and time for director-2-1-B
exec ssh root@128.221.252.68 "ntpd -q -g"
echo Finished synching director-2-1-B
echo

sleep 2

echo Changing date and time for director-2-2-A
exec ssh root@128.221.252.69 "ntpd -q -g"
echo Finished synching director-2-2-A
echo

sleep 2

echo Changing date and time for director-2-2-B
exec ssh root@128.221.252.70 "ntpd -q -g"
echo Finished synching director-2-2-B

sleep 2

echo

echo
echo Finished Synching SVT Cluster 2 directors with SMS.

该脚本似乎只在第一个 exec 命令之后完成。

2011 年 8 月 25 日星期四 12:40:44 EDT

将 GTS 集群 1 Controller 与 SMS 同步。

更改 Director-1-1-A 的日期和时间

任何帮助将不胜感激 =)

最佳答案

整点exec是到 替换 当前进程。在 shell 脚本中,这意味着 shell 被替换,exec 之后没有任何内容。被执行了。我疯狂的猜测是:也许你想用 & 来背景命令相反( ssh ... & )?

但是,如果您只想按顺序运行 sshs,每次等待它完成,只需删除 'exec' 字样。没有必要用 exec 表达“我想运行 this_command” .刚刚this_command会做的伎俩。

哦,把它变成 #!/bin/sh脚本;您的脚本中没有 bashism 或 linuxism。如果可以的话,避免 bashism 是一种很好的做法。这样,如果您的老板决定切换到 FreeBSD,您的脚本就可以不加修改地运行。

关于shell - 如何创建一个脚本来依次执行多个 "exec"命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7194018/

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