Shell – Bash script: do something one time inside a loop then stop, but continue looping

batterynotificationsscriptingshell-script

I have a Bash script that checks my laptop battery and sends me an Android notification (via shuttle, a Bash script I wrote that serves as a cli interface for the Pushbullet API). It works nicely, but it will repeatedly notify me to unplug my laptop when the battery reach 100%. Instead, I'd rather it just notify me of 100% charge once, and then that's it (i.e. continue checking but do not notify). I do, however, want it to repeatedly notify me when the battery is low, so that I remember to plug in the laptop (which is currently what it does).

I was thinking that this is possible using continue and break commands inside the loop, but I'm not sure if that's what I want (I'm not very familiar with those).

Anyway, I would appreciate suggestions as to how to best implement this using Bash. I suspect this is very simple, but I'm just not getting it for some reason.

Here is my script:

#! /bin/bash

while true;
do

percent=$(acpi | awk '{ print $4}' | sed -e 's/%//g' |  sed -e 's/,//g')

 if [ "$percent" -le "20" ];
then
    shuttle push note Chrome "Aurora: Plug in now" "Battery is at $percent percent"
fi  
if [ "$percent" -eq "100" ];
then
    shuttle push note Chrome "Aurora: Battery charged" "Battery is at $percent percent"
fi

sleep 7m
done

Best Answer

How about:

if [ "$percent" -eq 100 ] && [ "$full_flag" -eq 0 ];
then
    shuttle push note Chrome "Aurora: Battery charged" "Battery is at $percent percent"
    full_flag=1
fi
if [ "$percent" -lt 100 ];
then
    full_flag=0
fi
Related Question