Bash while do and if with Telegram API

Bash while do and if with Telegram API

我正在尝试监视 24/7 流。 为此,我正在监控我的树莓派的网络速度。

以千比特为单位的网络速度:

(old="$(</sys/class/net/eth0/statistics/rx_bytes)"; while $(sleep 1); do  now=$(</sys/class/net/eth0/statistics/rx_bytes); echo $((($now-$old)/1024)); old=$now; done)

输出在Kbit/s:

    216
    384
    288
    360
    336
    360

现在我想使用 Telegram API 在速度低于 100 时触发 Kbit/s

电报代码:

TELEGRAM=$(curl -s -X POST 'https://api.telegram.org/<BOT ID>:<API KEY>/sendMessage?chat_id=<ID>&text="Stream has problems"')

为此我需要一个 while do 循环和一个 if 条件。

亲切的问候, Goeks1

编辑////

$(old="$(</sys/class/net/eth0/statistics/rx_bytes)"; while $(sleep 1); do  now=$(</sys/class/net/eth0/statistics/rx_bytes); if (( (now-old)/1024 < 1000 )); then $TELEGRAM ; fi echo $((($now-$old)/1024)); old=$now; done)

我解决了。解决方案如下。

#!/bin/bash

OLD="$(</sys/class/net/eth0/statistics/rx_bytes)"
i=1
        while $(sleep 1m); do
NOW=$(</sys/class/net/eth0/statistics/rx_bytes)

if (( (NOW-OLD)/1024 > 100 )) ; then

        remainder=$(( i % 3 ))
        [ "$remainder" -eq 0 ] && curl -s -X POST 'https://api.telegram.org/<BOT ID>:<API KEY>/sendMessage?chat_id=<ID>&text="Stream has problems"'
        i=$(( i + 1 ))
fi
OLD=$NOW
done