循环完成后终端脚本不会中断
Terminal Script not breaking after loop complete
我正在尝试编写一个终端脚本;成功后我想触发一个功能,当它失败时我想发送警报。该脚本可以正常工作,除非成功后它会继续 运行 'done; 之后的命令;线。不确定正确的语法或正确的写法。
我只希望完成后的命令在失败时处理,而不是在成功时处理。
check_connection_wireguard(){
n=0
until [ "$n" -ge 1 ]
do
ping -c 1 $remoteIdrac &> /dev/null && echo 'Success' && break
n=$((n+1))
echo "Connection Failed Attempt" $n
sleep 15
done
ssh $localIp wg-quick down $wireguardInterface
ssh $localIp /usr/local/emhttp/webGui/scripts/notify -i alert -s \"Backup Remote Connection\" -d \"Backup Operation failed to connect\"
}
所以我用下面的代码解决了,只是添加了一个 if 语句,但想知道是否有比下面更优雅的解决方案。
check_connection_wireguard(){
n=0
until [ "$n" -ge 1 ]
do
ping -c 1 10.10.20.100 &> /dev/null && echo 'Success' && break
n=$((n+1))
echo "Connection Failed Attempt" $n
sleep 15
if [[ $n > 1 ]]; then
ssh $localIp wg-quick down $wireguardInterface
ssh $localIp /usr/local/emhttp/webGui/scripts/notify -i alert -s \"Backup Remote Connection\" -d \"Backup Operation failed to connect\"
fi
done
}
break
只是跳出循环。您希望 return
在成功时跳出函数:
check_connection_wireguard(){
n=0
until [ "$n" -ge 1 ]
do
ping -c 1 $remoteIdrac &> /dev/null && echo 'Success' && return
n=$((n+1))
echo "Connection Failed Attempt" $n
sleep 15
done
ssh $localIp wg-quick down $wireguardInterface
ssh $localIp /usr/local/emhttp/webGui/scripts/notify -i alert -s \"Backup Remote Connection\" -d \"Backup Operation failed to connect\"
}
我正在尝试编写一个终端脚本;成功后我想触发一个功能,当它失败时我想发送警报。该脚本可以正常工作,除非成功后它会继续 运行 'done; 之后的命令;线。不确定正确的语法或正确的写法。
我只希望完成后的命令在失败时处理,而不是在成功时处理。
check_connection_wireguard(){
n=0
until [ "$n" -ge 1 ]
do
ping -c 1 $remoteIdrac &> /dev/null && echo 'Success' && break
n=$((n+1))
echo "Connection Failed Attempt" $n
sleep 15
done
ssh $localIp wg-quick down $wireguardInterface
ssh $localIp /usr/local/emhttp/webGui/scripts/notify -i alert -s \"Backup Remote Connection\" -d \"Backup Operation failed to connect\"
}
所以我用下面的代码解决了,只是添加了一个 if 语句,但想知道是否有比下面更优雅的解决方案。
check_connection_wireguard(){
n=0
until [ "$n" -ge 1 ]
do
ping -c 1 10.10.20.100 &> /dev/null && echo 'Success' && break
n=$((n+1))
echo "Connection Failed Attempt" $n
sleep 15
if [[ $n > 1 ]]; then
ssh $localIp wg-quick down $wireguardInterface
ssh $localIp /usr/local/emhttp/webGui/scripts/notify -i alert -s \"Backup Remote Connection\" -d \"Backup Operation failed to connect\"
fi
done
}
break
只是跳出循环。您希望 return
在成功时跳出函数:
check_connection_wireguard(){
n=0
until [ "$n" -ge 1 ]
do
ping -c 1 $remoteIdrac &> /dev/null && echo 'Success' && return
n=$((n+1))
echo "Connection Failed Attempt" $n
sleep 15
done
ssh $localIp wg-quick down $wireguardInterface
ssh $localIp /usr/local/emhttp/webGui/scripts/notify -i alert -s \"Backup Remote Connection\" -d \"Backup Operation failed to connect\"
}