I've been trying to make a script for battery notifications for 15%, 10% and 5%. When the battery reaches those levels, it displays a notification saying to change my laptop. I made a variable called runOnce to state whether or not the script has already executed the if-statements once. If runOnce is false, run the if-statements inside a while-loop and if one of them is true, execute the commands and then set runOnce to true, stopping the while-loop. However, since I need the whole code to loop, so it also resets runOnce back to false. So, if the battery level is still the same, it repeatedly executes those commands. I've tried different solutions, but I can't figure out how not to let the script reset the variable back to false.
while true; do
battery=$(cat /sys/class/power_supply/BAT0/capacity)
batteryStatus=$(cat /sys/class/power_supply/BAT0/status)
runOnce=false
if [ $batteryStatus != "Charging" ]; then
while [ "$runOnce" = false ]; do
runOnce=false
if [ $battery -eq 15 ]; then
notify-send --urgency=critical "Low Battery! (15%)" "Charge your laptop quickly."
espeak "Low battery. Please charge."
runOnce=true
elif [ $battery -eq 10 ]; then
notify-send --urgency=critical "Critical Battery! (10%)" "Charge your laptop now. Before you lose your data. Seriously."
espeak "Critical battery. Please charge."
runOnce=true
elif [ $battery -eq 5 ]; then
notify-send --urgency=critical "REALLY Critical Battery! (5%)" "Dude, your battery is going to go to the landfill soon."
espeak "Critical battery. Charge immediately."
runOnce=true
fi
done
fi
sleep 5
done