You are not logged in.

#1 2009-07-18 15:02:46

Allamgir
Member
Registered: 2009-06-11
Posts: 168

[SOLVED] Change output of parameters in bash command?

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

#2 2009-07-18 16:15:10

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Re: [SOLVED] Change output of parameters in bash command?

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

#3 2009-07-18 16:24:03

Allamgir
Member
Registered: 2009-06-11
Posts: 168

Re: [SOLVED] Change output of parameters in bash command?

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

#4 2009-07-18 16:38:05

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Re: [SOLVED] Change output of parameters in bash command?

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..... wink

Last edited by crouse (2009-07-18 16:39:29)

Offline

#5 2009-07-18 16:41:58

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Change output of parameters in bash command?

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)

Offline

#6 2009-07-18 16:42:58

Allamgir
Member
Registered: 2009-06-11
Posts: 168

Re: [SOLVED] Change output of parameters in bash command?

Thanks everyone! brisbin got it! the command works smile

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

#7 2009-07-18 16:59:39

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Re: [SOLVED] Change output of parameters in bash command?

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

#8 2009-07-18 17:14:30

Allamgir
Member
Registered: 2009-06-11
Posts: 168

Re: [SOLVED] Change output of parameters in bash command?

Yes! It works! I think I've got my dwm status bar just how I like it now smile

Here's a screenshot

th_2009-07-18-114638_1280x800_scrot.png

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

Board footer

Powered by FluxBB