This forum is in archive mode. You will not be able to post new content.

Author Topic: [Bash] Show battery status in prompt  (Read 1477 times)

0 Members and 1 Guest are viewing this topic.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
[Bash] Show battery status in prompt
« on: May 21, 2013, 12:18:04 PM »
This bash script that I wrote uses awk to get the number of bars to show how much the battery is loaded. It prints the bars in appropriate colors ranging from red to yellow to green to show the status. In addition it indicates if the battery is loading with the '~'.

Note that you maybe have to adjust the paths of BATTERY_DIR and ACAD_DIR to make it work. (This script is tested and works on Ubuntu 13.04)

Code: [Select]
#! /bin/bash

BATTERY_DIR=/sys/class/power_supply/BAT0
ACAD_DIR=/sys/class/power_supply/AC

BARS=$( awk '{print int( 5*($0/100) + 0.5) }' ${BATTERY_DIR}/capacity)

if [ $(cat ${ACAD_DIR}/online) -eq 1 ]; then
    echo -ne "~"
fi

if [ $BARS -gt 3 ]; then
    echo -ne "\[\033[1;32m\]" #green color
elif [ $BARS -gt 2 ]; then
    echo -ne "\[\033[1;33m\]" #yellow color
else
    echo -ne "\[\033[1;31m\]" #red color
fi

for (( c=1; c<=${BARS}; c++ ))
do
    echo -ne "|"
done

for (( c=${BARS} + 1; c<=5; c++ ))
do
    echo -ne " "
done

Old (Ubuntu 12.10)
Code: [Select]
#! /bin/bash

BATTERY_DIR=/proc/acpi/battery/BAT1
ACAD_DIR=/proc/acpi/ac_adapter/ACAD

BARS=$( awk '/last full capacity/ {c = $4}
/remaining capacity/ {print int( (5*($3/c)) + 0.5 )}' ${BATTERY_DIR}/info ${BATTERY_DIR}/state)

if grep -q on-line ${ACAD_DIR}/state; then
    echo -ne "~"
fi

if [ $BARS -gt 3 ]; then
    echo -ne "\[\033[1;32m\]" #green color
elif [ $BARS -gt 2 ]; then
    echo -ne "\[\033[1;33m\]" #yellow color
else
    echo -ne "\[\033[1;31m\]" #red color
fi

for (( c=1; c<=${BARS}; c++ ))
do
    echo -ne "|"
done

for (( c=${BARS} + 1; c<=5; c++ ))
do
    echo -ne " "
done

Now you can use this i.e. to show the battery status in your prompt.

Example what to put in your .bashrc: (I saved the bash script as battery.sh in my home)

Code: [Select]
PS1="<\[\033[0;34m\]\u:\w\[\033[0;m\]>-(`battery.sh`\[\033[0;m\]):\$ "
Which will look like this (full battery):



Or like this (low battery):

« Last Edit: June 23, 2013, 11:16:04 AM by Deque »

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: [Bash] Show battery status in prompt
« Reply #1 on: May 21, 2013, 12:35:16 PM »
Very sweet little script.
Only Im wondering about performace.
I like my terminals blazingly fast, as they should be.
Do you notice this ?

Maybe you could use a cronjob every x minutes and read from that for example.
Would keep things faster I can imagine.
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [Bash] Show battery status in prompt
« Reply #2 on: May 21, 2013, 02:42:00 PM »
Very sweet little script.
Only Im wondering about performace.
I like my terminals blazingly fast, as they should be.
Do you notice this ?

Maybe you could use a cronjob every x minutes and read from that for example.
Would keep things faster I can imagine.

Thanks. I don't recognize any difference in speed, though.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: [Bash] Show battery status in prompt
« Reply #3 on: May 21, 2013, 05:39:52 PM »
Nice but I don't use lynx and also I use Desktop. :P
« Last Edit: May 21, 2013, 05:59:38 PM by parad0x »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [Bash] Show battery status in prompt
« Reply #4 on: May 21, 2013, 06:44:17 PM »
Nice but I don't use lynx and also I use Desktop. :P

What is lynx?

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: [Bash] Show battery status in prompt
« Reply #5 on: May 21, 2013, 06:44:53 PM »
Nice but I don't use lynx and also I use Desktop. :P

lolwut

What does the usage of a text browser have to do with this?
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: [Bash] Show battery status in prompt
« Reply #6 on: May 22, 2013, 07:46:34 AM »

Offline zoup

  • Serf
  • *
  • Posts: 29
  • Cookies: 3
  • I don't understand anything here !
    • View Profile
Re: [Bash] Show battery status in prompt
« Reply #7 on: May 22, 2013, 11:43:25 AM »
Nice script. I have to try this out on my laptop with broken battery  ;D

:thumbs up:

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [Bash] Show battery status in prompt
« Reply #8 on: June 23, 2013, 10:36:02 AM »
I upgraded to Ubuntu 13.04 and encountered that the script doesn't work anymore.
So I adjusted the bash script. See above. For users of the LTS the old script is still there in spoiler tags.

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.