While Loop Errors
Page 1 of 1

Author:  kaivanes [ Sun Mar 13, 2011 1:04 pm ]
Post subject:  While Loop Errors

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?

/Users/xxxxx/Desktop/ line 24: syntax error near unexpected token `done'
/Users/xxxxx/Desktop/ line 24: `done'

Code below.

#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]*.$`
   echo $PERCENT
   if[$PERCENT -le $1]
      osascript -e 'tell application "System Events" to sleep'
   elif[$PERCENT -le $2]
      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'
   sleep $3

Author:  Patsie [ Sun Mar 13, 2011 11:36 pm ]
Post subject:  Re: While Loop Errors

if[$PERCENT -le $1]

needs some more spaces
if [ $PERCENT -le $1 ]

same with the elif part.

Author:  kaivanes [ Mon Mar 14, 2011 10:04 am ]
Post subject:  Re: While Loop Errors

Thanks for the tip, but sadly that did not fix the problem :(

Also, many bonus points for having a beta terror as your avatar :)

Author:  SuperFly [ Tue Mar 15, 2011 4:31 am ]
Post subject:  Re: While Loop Errors

Shutdown your computer and take the power cord with you every day. Get new roommates.

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group •