gpt4 book ai didi

linux - 使用 shell,如何跳到 for 循环中的下一个元素?

转载 作者:行者123 更新时间:2023-12-02 19:44:19 25 4
gpt4 key购买 nike

这是我的脚本应该执行的操作:它对导入的文本文件的每一行的 IP 地址执行 5 次 ping 操作。如果 5 次中有 3 次 IP 无法访问,它会回显“已关闭!”并且(应该)继续下一个 IP... 除了我不知道如何配置这个“跳过”部分。这是我当前的代码:

FILE=file.txt
unreachableIP=0

while IFS='' read -r line || [ -n "$line" ]; do
set -- $line

for (( i=1; i<=5; i++ ))
do
echo "Ping $1 $i times"
ping -c 1 $1
if [ "$?" = 0 ]
then
echo "reachable"
else
echo "unreachable"
((unreachableIP++))
echo $unreachableIP
if [ $unreachableIP -eq 3 ]
then
echo "it's down!"
unreachableIP=0
fi
fi
done
done < $FILE

示例:如果 IP 地址第一次可达,第二次不可达,第三次不可达,第四次不可达,我希望脚本移动到下一个 IP(下一行),而不是第 5 次尝试 ping。这是 file.txt:

8.8.8.8 GoogleDNS
1.1.1.1 CloudFlareDNS
213.1.1.1 SomeFakeIPForDebug

感谢您的帮助。

编辑:我不想使用 break,因为它也会停止 ping 其他 IP...我希望它只停止 ping 当前 IP。

最佳答案

继续

The break and continue loop control commands [1] correspond exactly to their >counterparts in other programming languages. The break command terminates the loop >(breaks out of it), while continue causes a jump to the next iteration of the loop, >skipping all the remaining commands in that particular loop cycle.

参见 http://tldp.org/LDP/abs/html/loopcontrol.html

关于linux - 使用 shell,如何跳到 for 循环中的下一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59615727/

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