You are not logged in.
I have this small bash command that reads the output of "acpi" and prints parameters 3 and 4, which tells me the battery's state (Full, Charging, Discharging) and its percentage.
Here it is with the result:
$ acpi | awk '{ print $3, $4 }' | sed s/","//g
Full 100%
acpi itself displays this:
$ acpi
Battery 0: Full, 100%
#when discharging, this is the output:
$ acpi
Battery 0: Discharging, 98%, 03:34:18 remaining
#when charging, this is the output:
$ acpi
Battery 0: Charging, 88%, 00:22:02 until charged
What I would like to do is make it so instead of displaying "Full 100%", it shows "[F]100%", etc.
Full 100% to [F]100%
Discharging xx% to [D]xx%
Charging xx% to [C]xx%
This will save me some characters on my status bar when I pipe the information there. Unfortunately, I know nothing about bash scripting or awk, so I don't know how to change this.
Last edited by Allamgir (2009-07-18 17:15:58)
дɭɭɑӎɠїɾ
Offline
I'm on a M$crap computer at work, so i can't test it, but "something" like this should work...syntax might be off a bit as I can't test it, but I'll post it anyway to get you/someone started.
#!/bin/bash
STATE=`acpi | awk '{print $3}' | sed s/","//g`
PERCENT=`acpi | awk '{print $4}' | sed s/","//g`
if [ $STATE="Full" ]; then
echo "[F]100%"
else
if [ $STATE="Discharging" ]; then
echo "[D]${PERCENT}"
else
echo "[C]${PERCENT}"
fi
fi
Last edited by crouse (2009-07-18 17:00:16)
Offline
Thanks for the reply! I put the script in ~/battery.sh to test it out. This is the output when I try to run it, though:
$ sh ~/battery.sh
/home/agi/battery.sh: line 2: print: command not found
/home/agi/battery.sh: line 3: print: command not found
/home/agi/battery.sh: line 7: syntax error near unexpected token `else'
/home/agi/battery.sh: line 7: ` else'
How can I fix this?
дɭɭɑӎɠїɾ
Offline
like i said, i'm not on a linux machine... but i had a few errors i caught, guess you could try again (I edited my orginal post), or i'll work on it once i get home.....
Last edited by crouse (2009-07-18 16:39:29)
Offline
i would do it slightly differently:
$ acpi | awk '{print $3 $4}' | sed 's/\,//g;s/Full/\[F\]/g;s/Charging/\[C\]/g;s/Discharging/\[D\]/g'
[F]100%
you could also do this [slightly more legible] version
acpi | awk '{print $3, $4}' | while read state perc; do
case "$state" in
"Full") echo "[F]$perc" ;;
"Charging") echo "[C]$perc" ;;
"Discharging") echo "[D]$perc" ;;
esac
done
both of these are untested (no acpi battery info on a desktop) but i'm confident.
/edit: your errors i believe are from awk '{ print... which should be awk '{print... no space there.
Last edited by brisbin33 (2009-07-18 16:55:00)
//github/
Offline
Thanks everyone! brisbin got it! the command works
I'm going to test this in my .xinitrc. Hopefully that works fine, too. (I don't know why it wouldn't)
Last edited by Allamgir (2009-07-18 16:44:35)
дɭɭɑӎɠїɾ
Offline
Glad it's working, I like brisbin33's case solution the best. I'm all for "readability", the awk/sed mess while readable, isn't nearly as easy to decipher in a year or so when your trying to figure out what it does.
Offline
Yes! It works! I think I've got my dwm status bar just how I like it now
Here's a screenshot
And here's my completed .xinitrc:
#!/bin/sh
#
# ~/.xinitrc
feh --bg-scale /home/agi/Images/Good\ Wallpapers/linuxwallpaper42.png
urxvtd -q -f -o &
xbindkeys &
xset +dpms &
xsetroot -cursor_name left_ptr
#exec xmonad
#####DWM#####
network(){
iwconfig wlan0 2>&1 | grep -q no\ wireless\ extensions\. && {
echo wired
exit 0
}
essid=`iwconfig wlan0 | awk -F '"' '/ESSID/ {print $2}'`
stngth=`iwconfig wlan0 | awk -F '=' '/Quality/ {print $2}' | cut -d '/' -f 1`
bars=`expr $stngth / 10`
case $bars in
0) bar='[-------]' ;;
1) bar='[#------]' ;;
2) bar='[##-----]' ;;
3) bar='[###----]' ;;
4) bar='[####---]' ;;
5) bar='[#####--]' ;;
6) bar='[######-]' ;;
7) bar='[#######]' ;;
*) bar='[--!!!--]' ;;
esac
echo $essid$bar
exit 0
}
volume(){
vol=$(amixer get Master | awk -F'[]%[]' '/%/ {if ($7 == "off") { print "MM" } else { print $2 }}' | head -n 1)
echo Vol: $vol%
exit 0
}
while true; do
xsetroot -name "±`acpi | awk '{ print $3 $4 }' | sed 's/\,//g;s/Full/\[F\]/g;s/Charging/\[C\]/g;s/Discharging/\[D\]/g'` Load: `cat /proc/loadavg | cut -c 1-14` `volume` `network` `date '+%a %m-%d-%Y %I:%M%p'`"
sleep 1
done &
exec dwm
Thank you!!!
дɭɭɑӎɠїɾ
Offline