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


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: 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 | # ]