BashScripts.org
http://bashscripts.org/forum/

While Loop Errors
http://bashscripts.org/forum/viewtopic.php?f=16&t=1334
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?

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

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

Code:
if[$PERCENT -le $1]

needs some more spaces
Code:
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 • http://www.phpbb.com