Bash Function to Get Battery Charge

I added these two functions to my bashrc.  The first one is used to get the battery charge, and the second returns the battery’s current capacity.

function bchrg {
      # Returns battery charge as a percentage.
      now=`cat /sys/class/power_supply/BAT0/energy_now`
      full=`cat /sys/class/power_supply/BAT0/energy_full`
      out=`echo $now/$full*100 | bc -l | cut -c 1-5`
      echo "Charge: "$out"%"
  }
  
  function bcap {
      # Returns battery capacity as a percentage.
      design=`cat /sys/class/power_supply/BAT0/energy_full_design`
      current=`cat /sys/class/power_supply/BAT0/energy_full`
      out=`echo $current/$design*100 | bc -l | cut -c 1-5`
      echo "Capacity: "$out"%"
  }

About these ads

0 Responses to “Bash Function to Get Battery Charge”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s





Follow

Get every new post delivered to your Inbox.

%d bloggers like this: