真的很抱歉再次惹恼您,但看来我要完成了。我的目标是创建一个bash脚本,以检查IP地址是否仍在线或正在进行清理,如果不是,则关闭系统。我正在使用的脚本看起来像这样
#!/bin/bash
hosts=(
10.10.0.100 #Client 1
10.10.0.101 #Client 2
10.10.0.102 #Client 3
10.10.0.103 #Client 4
10.10.0.104 #Client 5
)
for host in "${hosts[@]}"; do
if ping -c 1 -i 1 "$host" >/dev/null; then
echo "No Shutdown - At least one PC ($host) is online"
exit 0
fi
done
echo "No PC is online - Shutdown"
bash shutdown -p now
我进行了一些研究,找到了以下命令,以检查我的清理是否正在进行中
if [ $(zpool status | grep 'scrub in progress') ]; then
echo "No Shutdown - Scrub in progess"
exit 0
fi
但是我在将两者结合时遇到问题。我希望我的脚本首先检查IP,如果它们都处于脱机状态,然后在关闭计算机之前检查清理。因此,两个if-case都必须为false(ips脱机且未进行清理),但应按时间顺序进行处理,并且如果第一个if-case返回IP处于联机状态,则脚本应停止。
也许有人可以帮助我吗?
对我来说,答案很简单:
hosts=(
10.10.0.100 #Client 1
10.10.0.101 #Client 2
10.10.0.102 #Client 3
10.10.0.103 #Client 4
10.10.0.104 #Client 5
)
for host in "${hosts[@]}"; do
if ping -c 1 -i 1 "$host" >/dev/null; then
echo "No Shutdown - At least one PC ($host) is online"
exit 0
fi
done
if [ $(zpool status | grep 'scrub in progress') ]; then
echo "No Shutdown - Scrub in progess"
exit 0
fi
echo "No PC is online and Scrub is not in progress - Shutdown"
bash shutdown -p now
还是我错过了要点?
您可以使用!
取消退出状态>
if ! ping -c 1 -i 1 "$host" >/dev/null; then if ! [[ $(zpool status | grep 'scrub in progress') ]]; then echo "No PC is online - Shutdown" bash shutdown -p now fi fi
这基本上意味着没有
!
的反面>嵌套将是类似的。
for host in "${hosts[@]}"; do if ping -c 1 -i 1 "$host" >/dev/null; then echo "No Shutdown - At least one PC ($host) is online" exit 0 elif ! ping -c 1 -i 1 "$host" >/dev/null; then if ! [[ $(zpool status | grep 'scrub in progress') ]]; then echo "No PC is online - Shutdown" bash shutdown -p now fi fi don
请参见
help test