'update value in bash script

i have stuck code. i make bash script to send Notification If file Size changed

#!/bin/bash

#File Embeded
test_file=/home/optimus/bot_test/dump.txt
msg_caption=/tmp/telegram_msg_caption.txt

#initialize
initCount=0

#checkLog
#cmd="ls /var/log/snort/* | wc -l"
cmd=$(wc -c "dump.txt" | awk '{print $1}')

#Chat ID and TOKEN Telegram
chat_id="xxxx"
token="xxx"

#Send Alert
function sendAlert
{
        curl -s -F chat_id=$chat_id -F caption="$caption" -F document=@$test_file  https://api.telegram.org/bot$token/sendDocument > /dev/null 2&>1
}

#Monitoring DoS Server
while true
do
    #GetLastCount
    echo "Start Execute"
    lastCount=$cmd
    echo before_last $lastCount #ex 100 #after reset 0
    echo before_init $initCount #ex 0
    echo "--------------------"
    if(($(($lastCount)) > $initCount));
       then
        echo "Running Script..." #debug
        echo -e "Halo Admin\nTerjadi Indikasi Penyerangan DoS!!!" > $msg_caption #set Caption / Pesan
        caption=$(<$msg_caption) #set Caption
        sendAlert #Panggil Fungsi

        #error disini
        initCount=$lastCount
        lastCount=0 #reset

        echo after_last $lastCount
        echo after_Init $initCount
        echo "==================="
        rm -f $msg_caption
        sleep 5 #delay Proses jika ada indikasi
    fi
    sleep 5 #delay proses jika tidak ada indikasi
done

when code first running, send First Notification, and if file size changed , value in code not updated example i use dump.txt to object size changed

how to get Last Size from dump.txt after echo "Start Execute"

conclusion every 5 second, lastCount get last size File updated



Solution 1:[1]

The problem in your code is that the cmd=$(wc -c "dump.txt" | awk '{print $1}') line executes only once.

Instead, you should use:

#....
do
#...
   lastCount=$(wc -c "dump.txt" | awk '{print $1}')
#...

Or make the cmd a function or store in a string and evaluate the string. The way you wrote, it is only evaluated in the beginning of your script and you use the same value afterward.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ali Khalili