从循环中获取旧值

时间:2018-06-04 23:02:14

标签: bash shell

如果数字的差异大于1000,我想每3秒检查一次。如何获得旧值,例如:16598,每次?

while true; do 
    testbro=$(wc -l < /home/web/log/access.log)
    echo $testbro
    sleep 3 
done

按需要输出:

16414
16471
16533
16598
16666

1 个答案:

答案 0 :(得分:2)

如果您打算在3秒钟内检测文件何时增长&gt; = 1000行,则可以执行以下操作:

#!/bin/bash

last_size=$(wc -l < /home/web/log/access.log)
while true; do
  sleep 3
  curr_size=$(wc -l < /home/web/log/access.log)
  if ((curr_size - last_size >= 1000)); then
    echo "$curr_size"
  fi
  last_size=$curr_size
done