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...]
maths inside a script, what's wrong here
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
maths inside a script, what's wrong here
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: maths inside a script, what's wrong here
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
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
Re: maths inside a script, what's wrong here
Try 'cut': here's a line from a .conkyrc
$ sensors -f
returns:
"Core 0: +104.0°F (high = +168.8°F, crit = +212.0°F)"
The 'cut' reduces it to "104.0".
Code: Select all
CPU0: ${cpu cpu1}% : ${execi 8 sensors -f | grep 'Core 0' | cut -c15-21 }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?
Your data and OS are backed up....right?
Re: maths inside a script, what's wrong here
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'
#!/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'
Re: maths inside a script, what's wrong here
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:~$
dale@cutermaster:~$ acpi
Battery 0: Discharging, 17%, 01:02:05 remaining
dale@cutermaster:~$ acpi | awk -F ", " '{print $2}'
17%
dale@cutermaster:~$