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


Click here to return to the 'See battery percentage in Terminal and GeekTool' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
See battery percentage in Terminal and GeekTool
Authored by: lihtox on Feb 06, '10 06:43:48AM
Here's an easier to read form (you can paste it into the terminal even with the line breaks, although you can also remove them and stuff them into one line if you like. A multiple line command is no trouble for GeekTool.)

ioreg -l |awk 'BEGIN{FS="=";max=0;cur=0;}
$1~/CurrentCapacity/{cur=$2} 
$1~/MaxCapacity/{max=$2}
END{if (max>0) {printf "%.2f%%\n",cur/max*100} else {print "?"}}'
One modification I made is to define the "field separator" to "=" (FS="="), which tells awk that the columns it reads are separated by equals signs, rather than the default tab character. This should be less fragile in case the format of ioreg -l changes for some reason.

[ Reply to This | # ]