Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

i just fresh-installed Lion, and i am in the process of seeing up my system. i am using this function (found somewhere online...) to show it in the prompt :

battery_status()
{
    maxcap=`ioreg -l | grep Capacity | awk '/MaxCapacity/{print $5}'`
    curcap=`ioreg -l | grep Capacity | awk '/CurrentCapacity/{print $5}'`

    curcap=`echo $curcap*1000|bc`
    proc=`echo $curcap / $maxcap |bc`;

    len=`echo ${#proc}`;

    proc_num=${proc:0:3}
    proc_dec=${proc:3:4}

    proc_num_len=`echo ${#proc_num}`;

    if [ $proc_num_len == "3" ]; then
            if [ $proc_num != "100" ]; then
                    proc_num=${proc:0:2}
                    proc_dec=${proc:2:3}
            fi
    fi

    echo $proc_num.$proc_dec%
}

And i generate my prompt like this :

_PS1user="\u@"
_PS1host="$txtred\h"
_PS1time="$txtwht | \t | "
_PS1battery="`battery_status`"
_PS1end="$txtrst\n\w › "

export PS1="$_PS1user$_PS1host$_PS1time$_PS1battery$_PS1end"

unset _PS1user _PS1host _PS1time _PS1battery _PS1end

However, since Lion, the battery Part is not updated every time the prompt appears again. For example, if i do a ls and wait for the battery to drained and then do another command in the same window, without having re source my .bashrc, the battery stays the same. does anyone know how to have it updated without having to resource my .bashrcfile resourced every time ?

(By the way, this is how my prompt looks like.)

share|improve this question

2 Answers 2

up vote 1 down vote accepted

The above script is so terribly written it brings tears to my eyes. Considering you're running it on every prompt, you may not want to fork so much.

Awk can do all of the above in one line:

ioreg -l | awk '/MaxCapacity/ {max=$5} /CurrentCapacity/ {cur=$5} END {printf "%.1f%%", cur/max*100}'

(sorry, not enough reputation to comment or I would've done that instead)

share|improve this answer
    
Thanks ! Actually, I found this script online, i don't even understand half of it. –  Aliou Aug 19 '11 at 6:35

You have to escape it, if you want it to rerun every time it gets called. It can easiest be done with the dollar-sign.

_PS1battery="\$(battery_status)"
share|improve this answer
    
So simple... Thanks ! –  Aliou Aug 11 '11 at 23:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.