Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Scripts: battery level and nicer uptime UNIX
I found many scripts in my documents folder, and I found two which are great for text-mode logins, which I'm learning to get used to in case of the need for damage repair. One prints the uptime nicely, and the other gives the battery level.

Read the rest of the hint for the code snippets...

[robg adds: I cannot get the battery script to run on my PowerBook, but the uptime script works. If anyone has a fix for the battery script, please post it as a comment and I'll edit it into the original hint.]

Battery level:

#!/bin/bash                                             
                                                        
if [ -x /usr/sbin/ioreg ] ; then
  ioreg -p IODeviceTree -n "battery" -w 0 | \
  sed -ne '/| *{/,/| *}/ {
    s/^[ |]*//g
    /^[{}]/!p  
  }' | \                                          
  awk '/Battery/ {                                
    gsub("[{}()\"]","", $3)                 
    gsub(","," ",$3)                        
    split($3,ct," ")                        
    sub(".*=","",ct[4])                     
    sub(".*=","",ct[5])                     
    print("Battery:",100*ct[5]/ct[4],"%")   
  }'                                              
fi
Uptime:

#!/bin/bash

uptime \
| awk '{
  # chops off "up" and everything before it:
  sub(/.*up[ ]+/,"",$0)
  # chops off ", # users" and everything after it:       
  sub(/,[ ]+[0-9]+ user.*/,"",$0)
  # cleans up extra spaces, i think:    
  sub(/,/,"",$0)
  # obvious enough, prints the results                       
  print("Uptime:",$0)
}'
Save these into Textedit on the Desktop (for convenience) as batt.txt and up.txt, and then do:

-- If you don't have a bin directory, do this first:
% mkdir ~/bin

% mv ~/Desktop/batt.txt ~/bin/batt
% mv ~/Desktop/up.txt ~/bin/up
% chmod u+x ~/bin/batt
% chmod u+x ~/bin/up
Next, put this line in the setup files that you 'source' at login. For tsch, this can be either .tcshrc or .login; for bash either .bash_profile or .profile:

export PATH="$PATH:$HOME/bin"
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[9,018 views]  

Scripts: battery level and nicer uptime | 10 comments | Create New Account
Click here to return to the 'Scripts: battery level and nicer uptime' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Scripts: battery level and nicer uptime
Authored by: LazLong on Oct 16, '03 09:25:37PM
For command-line type battery and charge information, I highly recommend battery_1.0. You can get it from: http://www.mitt-eget.com/software/macosx/

[ Reply to This | # ]
Scripts: battery level and nicer uptime
Authored by: BobHarris on Oct 16, '03 09:27:24PM

The script works find for me "As soon as I removed any trailing spaces after the line continuation back slashes"

Bob Harris



[ Reply to This | # ]
Scripts: battery level and nicer uptime
Authored by: LazLong on Oct 16, '03 09:29:42PM

Oops. Looks like they've updated the battery cmd-line tool in the last couple days. It's now version 1.1, and is available at the same URL I posted in the above message.

Laz



[ Reply to This | # ]
Scripts: battery level and nicer uptime
Authored by: jonn8n on Oct 17, '03 12:24:44AM
Here's a link to an AppleScript that gets battery info and is a bit more user friendly:

http://homepage.mac.com/jonn8/as/dist/Battery_Info.hqx

Jon

[ Reply to This | # ]
Scripts: battery level and nicer uptime
Authored by: cynikal on Oct 17, '03 01:43:11AM

what's wrong with /usr/bin/uptime?



[ Reply to This | # ]
Scripts: battery level and nicer uptime
Authored by: mervTormel on Oct 17, '03 01:46:39AM

$ echo uptime: `uptime | cut -d, -f1 | colrm 1 10`
uptime: 5 hrs



[ Reply to This | # ]
Scripts: battery level and nicer uptime
Authored by: greggo on Oct 17, '03 09:50:52AM

You could also use this for a nice uptime output...

echo Up Time = `uptime | awk '{print $3}' | tr -d ,` \(hours:minutes\)
-->Up Time = 17:52 (hours:minutes)



[ Reply to This | # ]
Scripts: battery level and nicer uptime
Authored by: mkhaw on Oct 17, '03 10:24:31AM

sed is harder to read, but here's more efficient pipeline to show the % battery charge:

#!/bin/sh

# use sed to print only the line of ioreg's output containing
# "Battery", keeping only the values for Capacity and Current;
# use bc to calculate 100*Current/Capacity, scaling to
# 2 decimal places; format the result using sed

ioreg -p IODeviceTree -n "battery" -w 0 | sed -ne \
'/Battery/s@^.*Capacity"=\([0-9]*\),"Current"=\([0-9]*\).*@scale=2; \2\*100/\1@p' \
| bc -l | sed -e 's/\(.*\)/Battery: \1%/'

...and one for uptime:

#!/bin/sh

# use sed to reformat output from uptime to discard
# text preceding and through " up " and text following
# and including ", ? users", where ? is 1 or more digits,
# then insert the prefix "Uptime: "

uptime | sed 's@.* up \(.*\), [0-9]* users.*@Uptime: \1@'


[ Reply to This | # ]
Scripts: battery level and nicer uptime
Authored by: mkhaw on Oct 17, '03 10:39:00AM

Hey, where did all the backslashes disappear to? Let's try again:

Battery % charge:
#!/bin/sh

# use sed to print only the line of ioreg's output containing
# "Battery", keeping only the values for Capacity and Current;
# use bc to calculate 100*Current/Capacity, scaling to
# 2 decimal places; format the result using sed

ioreg -p IODeviceTree -n "battery" -w 0 | sed -ne \
'/Battery/s@^.*Capacity"=\([0-9]*\),"Current"=\([0-9]*\).*@scale=2; \2\*100/\1@p' \
| bc -l | sed -e 's/\(.*\)/Battery: \1%/'
Uptime:
#!/bin/sh

# use sed to reformat output from uptime to discard
# text preceding and through " up " and text following
# and including ", ? users", where ? is 1 or more digits,
# then insert the prefix "Uptime: "

uptime | sed 's@.* up \(.*\), [0-9]* users.*@Uptime: \1@'


[ Reply to This | # ]
Scripts: battery level and nicer uptime
Authored by: mkhaw on Oct 17, '03 10:42:13AM

Everything looked OK in preview, but once I hit the "Submit Comment", the backslashes got stripped out. Bleah!



[ Reply to This | # ]