maths inside a script, what's wrong here

Forum rules
Before you post please read how to get help
Post Reply
tpprynn
Level 3
Level 3
Posts: 189
Joined: Fri May 27, 2011 7:09 pm

maths inside a script, what's wrong here

Post by tpprynn »

I have only dipped into using bash but have been trying to use this script as a startup script using the DWM window manager (with Slim as display manager) - the stuff below shows up on DWM's top-aligned 'panel'. My netbook has its battery info in what may be unusual places and it seems I'd need to calculate battery power remaining and to do this inside the script. Here's what I've got. Could someone please correct it for me? Obviously I've pasted together things from elsewhere that I three-quarters understand, and have left the hashed notes in and some of the strings/variables I've adapted. batnow and batfull are mine, and the battery information is in /sys/class/power_supply/BAT0/ and not /proc...

I'm assuming I need to divide batnow by batfull and multiply it by 100 to give a percentage of battery power but I must be getting the form wrong. The date is working and I'm not bothered about memfreak. Thanks for all help.

#!/bin/sh
# xsetroot is a way of putting stuff on the top bar
# the loop reads some information in to variables
# which xsetroot shoves out to the top bar every
# 10 seconds.
while true; do
# memory free, note use of backticks AND apostrophes in the line below
#memfreak2=`grep MemFree /proc/meminfo | awk '{ print $2 }'`;
#memfreak=$(( $memfreak2/1024 ));
#BAT=$( cat /proc/acpi/battery/BAT0/state | grep rem | awk '{ print $3 }' )
#batnow=$( cat /sys/class/power_supply/BAT0/energy_now )
#batfull=$( cat /sys/class/power_supply/BAT0/energy_full )
#BAT=$(( $batnow/$batfull )*100 ); this is the bit I need to solve I suppose
#use this till solved but only shows awkward 8 digit number
BAT=$( cat /sys/class/power_supply/BAT0/energy_now )
#time
CLK=$( date +'%a %b %d %R')
xsetroot -name "$BAT | $CLK "
sleep 10
done & # end of the loop
# background
sleep 1
feh --bg-fill '/home/tp/Pictures/bgx.jpg'

[I can't really mark this as solved as the solution was at odds with the title...]
Last edited by tpprynn on Wed Nov 06, 2013 4:47 am, edited 1 time in total.
tpprynn
Level 3
Level 3
Posts: 189
Joined: Fri May 27, 2011 7:09 pm

Re: maths inside a script, what's wrong here

Post by tpprynn »

I learn now that acpi -b does actually give the percentage I want and works on this netbook, wherever it gets this info. But it prints

Battery 0: Discharging, 17%, 01:02:05 remaining

which is too much detail. How could I trim this to either

17%

or

Battery: 17%

or even

01:02:05 remaining

The line in the script now reads: BAT=$(acpi -b {print $3} )

I know awk is involved and shaves off commas and words somehow but the brain fog is in place so far. Thanks
User avatar
Flemur
Level 18
Level 18
Posts: 8490
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: maths inside a script, what's wrong here

Post by Flemur »

Try 'cut': here's a line from a .conkyrc

Code: Select all

CPU0: ${cpu cpu1}% : ${execi 8 sensors -f | grep 'Core 0' | cut -c15-21 }F
$ sensors -f
returns:
"Core 0: +104.0°F (high = +168.8°F, crit = +212.0°F)"

The 'cut' reduces it to "104.0".
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
tpprynn
Level 3
Level 3
Posts: 189
Joined: Fri May 27, 2011 7:09 pm

Re: maths inside a script, what's wrong here

Post by tpprynn »

I kept trying out both cut - thanks for that - and awk until I thought I understood one enough to make it work. awk got there first although I'd like soon enough to understand both. (If someone wants to add the 'cut' equivalent to my line with 'awk' in that'd be nice.) This puts battery life as a percentage, date and time, and distro name and version on the DWM panel 'dmenu' and sets the wallpaper. (I was trying to avoid recompiling DWM to accommodate these commands and have managed it by calling this script from within my display manager Slim's /etc/slim.conf file.)

#!/bin/sh
# xsetroot is a way of putting stuff on the top bar
# the loop reads some information in to variables
# which xsetroot shoves out to the top bar every
# 10 seconds.
while true; do

#battery life
BAT=$(acpi | awk '{ print $4 }' | sed s/","//g)
#time
CLK=$( date +'%a %b %d %R')
#distribution
DIST=$( cat /etc/issue.net )

sleep 10
done & # end of the loop
# background
sleep 1
feh --bg-fill '/home/tp/Pictures/bgx.jpg'
lime
Level 1
Level 1
Posts: 7
Joined: Sat Sep 28, 2013 6:55 am
Location: Shoreline, WA

Re: maths inside a script, what's wrong here

Post by lime »

dale@cutermaster:~$ alias acpi='echo "Battery 0: Discharging, 17%, 01:02:05 remaining"'
dale@cutermaster:~$ acpi
Battery 0: Discharging, 17%, 01:02:05 remaining
dale@cutermaster:~$ acpi | awk -F ", " '{print $2}'
17%
dale@cutermaster:~$
Post Reply

Return to “Scripts & Bash”