Is this code well written? I'm not doing much, just rendering my battery status for my status bar but the code seems pretty long. I am new to Bash and Linux, so I thought this code could be improved somewhere.
#!/bin/bash
# Send a notification if battery is low, change status in info file accordingly
notify-low () {
# Battery isn't much but is charging (don't notify)
[ "$status" = "Charging" ] && return
# Battery critically low
if [ "$capacity" -le 10 ] && [ -z "$(grep critically-low $lowinfo)" ]; then
notify-send -i $HOME/.config/icons/critical-battery.png -u critical "Battery critically low!"
echo "critically-low" > $lowinfo
# Battery is low
elif [ "$capacity" -le 20 ] && [ -z "$(grep low $lowinfo)" ]; then
notify-send -i $HOME/.config/icons/low-battery.png -t 5500 "Battery low!"
echo "low" > $lowinfo
fi
}
battery="/sys/class/power_supply/BAT0"
status="$(cat "$battery/status")"
capacity="$(cat "$battery/capacity")"
# Get the low battery status from file ~/.cache/battery-low-status
lowinfo="$HOME/.cache/battery-low-status"
[ ! -e "$lowinfo" ] && touch $lowinfo
# Set charging icon and capacity icon
[ "$status" = "Charging" ] && charging_icon=" " || charging_icon=""
[ "$capacity" -gt 100 ] && capacity=100
if [ "$capacity" -lt 15 ]; then capacity_icon=' '
elif [ "$capacity" -lt 40 ]; then capacity_icon=' '
elif [ "$capacity" -lt 60 ]; then capacity_icon=' '
elif [ "$capacity" -lt 90 ]; then capacity_icon=' '
else capacity_icon=' '
fi
# Report low battery
[ "$capacity" -le 20 ] && notify-low
# Reset low battery information in these cases
([ "$capacity" -gt 20 ] || [ "$status" = "Charging" ]) && [ -n $(cat "$lowinfo") ] && echo "" > $lowinfo
printf "%s%s%d%%\n" "$charging_icon" "$capacity_icon" "$capacity"