gpt4 book ai didi

linux - Bash脚本错误服务器问题

转载 作者:太空宇宙 更新时间:2023-11-04 10:52:00 28 4
gpt4 key购买 nike

我有以下 bash 脚本

#!/bin/bash

servers="server1.... server2...."
seconds="3" # value for servers to differ (in seconds)
now=$(date +%s)
for server in $servers
do
ssh -l root ${server} "( date +%s )" > /tmp/.tmpdate
echo "-" $(date +%s) >> /tmp/.tmpdate
difference=$(awk -F '-' '{if ($1-$2 < 0) { print $2-$1 } else { print $1-$2} }' /tmp/.tmpdate)
if [[ $difference -le $seconds ]] ; then
echo $server - IN SYNC
else
echo $server - NOT IN SYNC
fi
done

我遇到了 difference=$(awk -F '-' '{if ($1-$2 < 0) { print $2-$1 } else { print $1-$2} }' /tmp/.tmpdate) 的语法错误导致脚本其余部分不准确的行。有什么想法吗?

最佳答案

我们可以简化一些:

#!/bin/bash 

servers="server1.... server2...."
seconds="3" # value for servers to differ (in seconds)
for server in $servers
do
remote=$(ssh -l root ${server} date +%s )
now=$(date +%s)
if [ "$remote" -gt "$now" ]
then
diff=$(expr $remote - $now)
else
diff=$(expr $now - $remote)
fi
echo $diff

if [[ $diff -gt $seconds ]] ; then
echo $server - NOT IN SYNC
else
echo $server - IN SYNC
fi
done

关于linux - Bash脚本错误服务器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30487292/

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