Code:
#!/bin/bash
#
# The computer tells the user to plug them in and shows a Growl notification every minute.
#
# Setup:
# 1. Install growlnotify
# 2. Adjust threshold in line 20 (here 656mAh)
# 3. Create line in crontab like this:
# * * * * * /path/to/script/battery_low.sh
#Name of the user.
name="Habib"
#Level of charge in % to warn the user at.
warnat="101"
#Is the computer plugged in?
pluggedin=`/usr/sbin/system_profiler SPPowerDataType | grep "Connected:"`
#Is the computer charging?
charging=`/usr/sbin/system_profiler SPPowerDataType | grep "Charging:"`
#Is the computer fully charged?
charged=`/usr/sbin/system_profiler SPPowerDataType | grep "Fully Charged:"`
#If the computer is plugged in and charging, do nothing.
if [["$pluggedin" == *Yes* && "$charging" == *Yes*]]
then
exit
#If the computer is plugged in and not fully charged, but not charging, tell the user.
else if [["$pluggedin" == *Yes* && "$charging" == *No* && "$charged" == *No*]]
then
say "Something is wrong. Your computer is plugged in and not fully charged, but it's not charging."
else
#Charge remaining in mAh.
rem=`/usr/sbin/system_profiler SPPowerDataType | grep -i "Charge remaining" | sed 's/[^0-9]//g'`
#Current battery capacity in mAh.
cap=`/usr/sbin/system_profiler SPPowerDataType | grep -i "Full Charge Capacity" | sed 's/[^0-9]//g'`
#Charge remaining in percent. -l flag makes result floating point.
remperc=`echo "$rem/$cap*100" | bc -l`
#This turns the value of remperc into an integer, so users don't hear a number with loads of digits after it.
remperc=`echo "$remperc" | awk '{printf("%d\n",$1 + 0.5)}'`
echo "$remperc"
if [ "$remperc" -lt "$warnat" ]
then
#/usr/local/bin/growlnotify -n batteryAlert -p High -m "Battery level low
($remperc%)"
say "$name, plug me in! I'm low on juice. I only have $remperc percent left."
fi
fi
fi
I'm kinda late for school, but if anyone can shed any light as to what's going on here, I'd be really grateful. The problem is, the script says "Plug me in", but even if the computer is plugged in. The if statements are clearly deficient. I don't know what I'm doing wrong. It does say the correct percentage level. I put it at 101 for testing purposes. I want it to warn me at 10% or less (so warn at would be 11), but only when the computer is unplugged.
I also get this output at runtime:
Code:
/Users/Habib/Expanded/battery_low_vague.sh: line 24: [[ Connected: Yes: command not found
/Users/Habib/Expanded/battery_low_vague.sh: line 28: [[ Connected: Yes: command not found
100