Find the answer to your Linux question:
Results 1 to 3 of 3
Add this to .bashrc and it will display the percent of the battery (charging/discharging) as well as the time remaining at the command prompt. Also displays it in the KDE ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie unchiujar's Avatar
    Join Date
    Oct 2006
    Posts
    194

    Talking Script for battery percent and time remaining at the command prompt (not a question)


    Add this to .bashrc and it will display the percent of the battery (charging/discharging) as well as the time remaining at the command prompt. Also displays it in the KDE konsole (only one I tested but it should work for gnome and xterm too)

    Original script here
    Enjoy!

    Code:
    # (c) 2003 Fabio 'farnis' Sirna <farnis@xxxxxxxxx>
    # modified by unchiujar
    # released under GPL3
    
    BAT_INFO="/proc/acpi/battery/BAT1/info"
    BAT_STATE="/proc/acpi/battery/BAT1/state"
    AC_STATE="/proc/acpi/ac_adapter/ACAD/state"
    function acpi_percent()
    {
      #if battery is present get info else display NO BATTERY
      if [ `cat $BAT_STATE | grep present: |cut -f18 -d\ ` = "yes" ]; then
      {
       #using last full capacity because it's more accurate then design capacity
       CAPACITY=`cat $BAT_INFO |grep "last full capacity:"|cut -f9 -d\ `
       #juice remaining
       LEVEL=`cat $BAT_STATE | grep remaining |cut -f8 -d\ `
       #rate of charge/discharge
       RATE=`cat $BAT_STATE | grep rate | cut -f14 -d\ `
       ACPI_PERCENT=`echo $(( $LEVEL * 100 / $CAPACITY ))`
       #if discharging calculate the time remaining by LEVEL/RATE 
       #if charging calculate the time remaining by (CAPACITY-LEVEL)/RATE 
       if [ `cat $BAT_STATE | grep charging | cut -f12 -d\ ` = "discharging" ]; then
            BAT_REMAINING=`echo  $LEVEL / $RATE  | bc -l | cut -c1-4 `
          else
            BAT_REMAINING=`echo  \( $CAPACITY - $LEVEL \) / $RATE  | bc -l | cut -c1-3 `
         fi
        #integer for comparison, if less then 1 (coresponds to 1 hour) display minutes
        #else display in hour format
       TEST=`echo $BAT_REMAINING\>1 | bc `
       if [ "$TEST" -eq "0" ]; then
             BAT_REMAINING=`echo  $BAT_REMAINING*60 | bc | cut -f1 -d.`m
        else
             BAT_REMAINING=`echo $BAT_REMAINING`h
        fi
       #if charged display FULL
       if [ "$LEVEL" -eq "$CAPACITY" ]; then
        echo FULL
       else
        echo $ACPI_PERCENT% $BAT_REMAINING
       fi
      }
     else echo "NO BATTERY"
     fi
    }
    #display + or - depending on the state of the battery charging/discharging
    function acpi_charge()
    {
     ACPI_CHARGE=`cat $AC_STATE | cut -f20 -d\ `
     case $ACPI_CHARGE in
           *on-line*)
             ACPI_CHARGE="+" ;;
           *off-line*)
             ACPI_CHARGE="-" ;;
         esac
         echo $ACPI_CHARGE
    }
    #change the color depending on the charge and other param
    function acpi_color()
       {
         if  [  "$(acpi_charge)"  =  "+"  ];  then
          {
           if [ `cat $BAT_STATE | grep present: |cut -f18 -d\ ` = "no" ]; then
            echo  "0;31"
           else echo  "1;32"
          fi
         }
         else
           case  $(acpi_percent)  in
              10?%)  echo  "0;32"  ;;
               9?%)  echo  "0;32"  ;;
               8?%)  echo  "0;32"  ;;
               7?%)  echo  "0;32"  ;;
               6?%)  echo  "0;32"  ;;
               5?%)  echo  "0;32"  ;;
               4?%)  echo  "0;33"  ;;
               3?%)  echo  "0;33"  ;;
               2?%)  echo  "0;33"  ;;
               1?%)  echo  "0;31"  ;;
                ?%)  echo  "0;31;5"  ;;
                 *)  echo  "0;35"  ;;
           esac
         fi
       }
    #set the prompt
    function  acpi_color_prompt
       {
         #PS1='\[\e[$(acpi_color)m\][$(acpi_charge)$(acpi_percent)][\t]\u:\w\$>\[\e[0;37m\] '
         #PS1="\[\e[$(acpi_color)m\]\h\$\[\e[0m\]  "
         PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\][\t]\[\e[$(acpi_color)m\][$(acpi_charge)$(acpi_percent)] \w \$\[\033[00m\] '
      echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007"
    }
    
       #  linux  console
       if  [  "$TERM"  =  "linux"  ];  then
         PROMPT_COMMAND=acpi_color_prompt
       fi
    case ${TERM} in
            xterm*|rxvt*|Eterm|aterm|kterm|gnome)
                    PROMPT_COMMAND=acpi_color_prompt
    esac
    
    
    function  echo_acpi
       {
         echo -n "($(acpi_charge)$(acpi_percent)) "
       }

  2. #2
    Linux Newbie rinjo's Avatar
    Join Date
    Feb 2007
    Location
    Bangalore, India
    Posts
    228
    Nice utility...
    Worth giving a try...
    Linux Rocks!!!!
    -- Rinjo
    Setup: Oses: Windows 7 HB and Fedora 17 dual boot Hardware: HP Pavilion G6-2005AX laptop, AMD A8 Quad Core, 4 GB RAM, 1.5 GB dual Graphics card, 500GB HDD.

  3. #3
    Linux Newbie unchiujar's Avatar
    Join Date
    Oct 2006
    Posts
    194
    Small change (the text console prompt didn't show up properly)

    Code:
    # (c) 2003 Fabio 'farnis' Sirna <farnis@xxxxxxxxx>
    # modified by unchiujar
    # released under GPL3
    
    BAT_INFO="/proc/acpi/battery/BAT1/info"
    BAT_STATE="/proc/acpi/battery/BAT1/state"
    AC_STATE="/proc/acpi/ac_adapter/ACAD/state"
    function acpi_percent()
    {
      #if battery is present get info else display NO BATTERY
      if [ `cat $BAT_STATE | grep present: |cut -f18 -d\ ` = "yes" ]; then
      {
       #using last full capacity because it's more accurate then design capacity
       CAPACITY=`cat $BAT_INFO |grep "last full capacity:"|cut -f9 -d\ `
       #juice remaining
       LEVEL=`cat $BAT_STATE | grep remaining |cut -f8 -d\ `
       #rate of charge/discharge
       RATE=`cat $BAT_STATE | grep rate | cut -f14 -d\ `
       ACPI_PERCENT=`echo $(( $LEVEL * 100 / $CAPACITY ))`
       #if discharging calculate the time remaining by LEVEL/RATE 
       #if charging calculate the time remaining by (CAPACITY-LEVEL)/RATE 
       if [ `cat $BAT_STATE | grep charging | cut -f12 -d\ ` = "discharging" ]; then
            BAT_REMAINING=`echo  $LEVEL / $RATE  | bc -l | cut -c1-4 `
          else
            BAT_REMAINING=`echo  \( $CAPACITY - $LEVEL \) / $RATE  | bc -l | cut -c1-3 `
         fi
        #integer for comparison, if less then 1 (coresponds to 1 hour) display minutes
        #else display in hour format
       TEST=`echo $BAT_REMAINING\>1 | bc `
       if [ "$TEST" -eq "0" ]; then
             BAT_REMAINING=`echo  $BAT_REMAINING*60 | bc | cut -f1 -d.`m
        else
             BAT_REMAINING=`echo $BAT_REMAINING`h
        fi
       #if charged display FULL
       if [ "$LEVEL" -eq "$CAPACITY" ]; then
        echo FULL
       else
        echo $ACPI_PERCENT&#37; $BAT_REMAINING
       fi
      }
     else echo "NO BATTERY"
     fi
    }
    #display + or - depending on the state of the battery charging/discharging
    function acpi_charge()
    {
     ACPI_CHARGE=`cat $AC_STATE | cut -f20 -d\ `
     case $ACPI_CHARGE in
           *on-line*)
             ACPI_CHARGE="+" ;;
           *off-line*)
             ACPI_CHARGE="-" ;;
         esac
         echo $ACPI_CHARGE
    }
    #change the color depending on the charge and other param
    function acpi_color()
       {
         if  [  "$(acpi_charge)"  =  "+"  ];  then
          {
           if [ `cat $BAT_STATE | grep present: |cut -f18 -d\ ` = "no" ]; then
            echo  "0;31"
           else echo  "1;32"
          fi
         }
         else
           case  $(acpi_percent)  in
              10?%)  echo  "0;32"  ;;
               9?%)  echo  "0;32"  ;;
               8?%)  echo  "0;32"  ;;
               7?%)  echo  "0;32"  ;;
               6?%)  echo  "0;32"  ;;
               5?%)  echo  "0;32"  ;;
               4?%)  echo  "0;33"  ;;
               3?%)  echo  "0;33"  ;;
               2?%)  echo  "0;33"  ;;
               1?%)  echo  "0;31"  ;;
                ?%)  echo  "0;31;5"  ;;
                 *)  echo  "0;35"  ;;
           esac
         fi
       }
    #set the prompt
    function  acpi_color_prompt
       {
         #PS1='\[\e[$(acpi_color)m\][$(acpi_charge)$(acpi_percent)][\t]\u:\w\$>\[\e[0;37m\] '
         #PS1="\[\e[$(acpi_color)m\]\h\$\[\e[0m\]  "
         PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\][\t]\[\e[$(acpi_color)m\][$(acpi_charge)$(acpi_percent)] \w \$\[\033[00m\] '
    }
    #adds extra information for graphical consoles
    case ${TERM} in
            xterm*|rxvt*|Eterm|aterm|kterm|gnome)
                    PROMPT_COMMAND=acpi_color_prompt
                    #sets up a proper title for graphical consoles
                     echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007"
                       ;;
              linux)
                   PROMPT_COMMAND=acpi_color_prompt
                   ;;       
    esac
    
    
    function  echo_acpi
       {
         echo -n "($(acpi_charge)$(acpi_percent)) "
       }
    If you have any suggestions for improvement let me know.

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •