Hi, I'm making a simple script to autosleep my computer when the battery gets to low (my roommates keep stealing my power cord and I come home to a crashed computer everyday >.<). The body of the program works, but in order to be effective it needs to run in an infinite loop to keep checking the battery status. I thought that a simple "while true; do .... done" would work, but it throws an error on the last line. Perhaps I'm missing something obvious?
Code:
/Users/xxxxx/Desktop/battery.sh: line 24: syntax error near unexpected token `done'
/Users/xxxxx/Desktop/battery.sh: line 24: `done'
Code below.
Code:
#!/bin/sh
# battery.sh
#
#Checks batter level every $3 seconds. Issues warning at $2% and auto sleeps at
#$1%. Set $1 to zero to disable autosleep.
while true; do
MAX=`ioreg -l | grep MaxCapacity | grep -o [0-9]*.$`
CURRENT=`ioreg -l | grep CurrentCapacity | grep -o [0-9]*.$`
PERCENT=$(($CURRENT*100/$MAX))
echo $PERCENT
if[$PERCENT -le $1]
then
osascript -e 'tell application "System Events" to sleep'
elif[$PERCENT -le $2]
then
osascript -e 'tell application "System Events"
set visible of (every process) to false
set visible of process "Finder" to false
display dialog "WARNING: System battery at $2%!" buttons {"OK"} default button 1
end tell'
fi
sleep $3
done