linux - 如何在 bash 的 if 条件下验证时间?

标签 linux bash ubuntu terminal

我希望您能帮助我提供有关我的代码的想法,因为我不知道如果脚本在 if 条件下在 20 分钟内输入两次,我该如何验证。

rc=0
while [ $rc -ne 1 ]; do
do
  ping 192.168.1.89

  if [ "$?" -ne "0" ]; then
     sleep 600 # this are 10 minutes
     # here i need to validate if this script enters two times followed in 20 minutes
     continue

  else
     continue
  fi

done

欢迎任何帮助,谢谢

最佳答案

您似乎正在监控一个 IP 是否断开连接,并且您想要在相隔 10 分钟的两次 ping 失败时采取措施。为此,您只需使用一个计数器即可。

failures=0
rc=0
while [ $rc -ne 1 ]; do
do
  ping 192.168.1.89
  if [ $? -ne 0 ]; then
     ((failures++))
     echo Ping failures: $failures
     if (( failures < 2 )); then
         sleep 600
     else
         echo Max failures hit
     fi
  else
     failures=0
  fi
done

您可能需要修改 ping 命令行以发送最大数量的 ping(例如,使用 -c 3 最多发送 3 个)。您可能希望将 ping 输出重定向到 /dev/null

关于linux - 如何在 bash 的 if 条件下验证时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36928797/

相关文章:

c++ - Linux ext3 和 ext4 文件 - 兼容性问题

linux - 从 Yocto build 手动构建内核源代码

php - 在 LINUX 上将 PDF ( v > 1.5 ) 转换为图像

bash 序列 : wait for output, 然后开始下一个程序

linux - munin 没有显示任何图表

git - 将 ~/.gitconfig 中的 github.token 别名为 shell 命令

node.js - 使用 node.js 以编程方式添加 samba 用户

mysql - 允许远程访问 MySQL、Ubuntu

apache - 找不到虚拟主机对象

ubuntu - 如何使用 linux/ubuntu 从 canmore gporter gp-102+ 获取 gps 跟踪日志