Register
It is currently Fri Oct 24, 2014 4:18 am

While Loop Errors


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Sun Mar 13, 2011 1:04 pm   

Joined: Fri Feb 11, 2011 2:03 pm
Posts: 4
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


Top
 Profile  
 PostPosted: Sun Mar 13, 2011 11:36 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Code:
if[$PERCENT -le $1]

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

same with the elif part.


Top
 Profile  
 PostPosted: Mon Mar 14, 2011 10:04 am   

Joined: Fri Feb 11, 2011 2:03 pm
Posts: 4
Thanks for the tip, but sadly that did not fix the problem :(

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


Top
 Profile  
 PostPosted: Tue Mar 15, 2011 4:31 am   
User avatar

Joined: Wed Nov 17, 2010 1:02 pm
Posts: 9
Location: North Bethesda, MD
Shutdown your computer and take the power cord with you every day. Get new roommates.


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP