Fatdog Battery Applet 2

Moderators: kirk, jamesbond, p310don, JakeSFR, step, Forum moderators

Post Reply
User avatar
dr__Dan
Posts: 77
Joined: Tue Jul 28, 2020 5:06 am
Has thanked: 45 times
Been thanked: 26 times

Fatdog Battery Applet 2

Post by dr__Dan »

Greetings all,

Copied over from the murga-linux forum.

From my other post:
I've wanted to get more information from the battery icon in the tray for a
long time, so I started adding to the fatdog-battery-applet.sh script to
make it more to my liking. I don't know that much about shell
scripting, but I know more now than before. The script attached below
seems to work fairly well. I've tried it on Dell, Gateway/Acer, HP, and
Toshiba laptops. I'd appreciate any additional brand-based evaluation.

The script wants configuration files lowbatt, criticalbatt and shutdown
in ~/.config/battery_level/, and refers to an as-yet-nonexistant
fatdog-power-settings.sh which I envision as being able to set a variety
of battery and power usage parameters. Since it doesn't yet exist, sit
ignores it.

The code could be more elegant, and I plan to improve it, or incorporate
any suggestions I receive, over time. For example, I'll eventually want
to simplify the tooltip section, perhaps use a single configuration
file, and improve the battery health presentation.

If it is successful and practical, it could be packaged with preset
parameters in the appropriate locations, but I haven't figured that bit
out yet.

Any feedback is appreciated.
step has replied with some basic and very helpful information that I
will incorporate soon and post back. In the mean time, as I wrote
before, any feedback is appreciated.

Note: depends on dash and sit.

It had over 800 views, but no responses.

Dan
Attachments
fatdog-battery-applet-2.sh.tar.gz
(2.53 KiB) Downloaded 45 times
charging.png
charging.png (14.42 KiB) Viewed 725 times
draining.png
draining.png (10.04 KiB) Viewed 725 times

9 years on with Fatdog64. :D

User avatar
dr__Dan
Posts: 77
Joined: Tue Jul 28, 2020 5:06 am
Has thanked: 45 times
Been thanked: 26 times

Re: Fatdog Battery Applet 2

Post by dr__Dan »

...And so I attempted to incorporate step's ideas. I've attached the modified code. Meld tells me that it is identical other than the modifications, but I get this set of errors when I run it in terminal:

Code: Select all

# fatdog-battery-applet-2-step-up.sh
/usr/bin/fatdog-battery-applet-2-step-up.sh: 178: /usr/bin/fatdog-battery-applet-2-step-up.sh: cannot open /sys/class/power_supply/*/charge_now: No such file
/usr/bin/fatdog-battery-applet-2-step-up.sh: 157: /usr/bin/fatdog-battery-applet-2-step-up.sh: cannot open /sys/class/power_supply/*/charge_full: No such file
/usr/bin/fatdog-battery-applet-2-step-up.sh: 161: /usr/bin/fatdog-battery-applet-2-step-up.sh: cannot open /sys/class/power_supply/*/charge_full_design: No such file
/usr/bin/fatdog-battery-applet-2-step-up.sh: 186: /usr/bin/fatdog-battery-applet-2-step-up.sh: cannot open /sys/class/power_supply/*/online: No such file
/usr/bin/fatdog-battery-applet-2-step-up.sh: 188: /usr/bin/fatdog-battery-applet-2-step-up.sh: cannot open /sys/class/power_supply/*/capacity: No such file
/usr/bin/fatdog-battery-applet-2-step-up.sh: 126: /usr/bin/fatdog-battery-applet-2-step-up.sh: cannot open /sys/class/power_supply/*/charge_now: No such file
/usr/bin/fatdog-battery-applet-2-step-up.sh: 60: /usr/bin/fatdog-battery-applet-2-step-up.sh: Bad substitution
# 
I have the original version running, and it can happily access those files. I get the same errors in 802 and 810.

Again, any feedback is appreciated. This may be simple, but it's beyond me.
Attachments
fatdog-battery-applet-2.1.sh.tar.gz
(2.78 KiB) Downloaded 42 times

9 years on with Fatdog64. :D

step
Posts: 512
Joined: Thu Aug 13, 2020 9:55 am
Has thanked: 50 times
Been thanked: 182 times
Contact:

Re: Fatdog Battery Applet 2

Post by step »

Dan, without looking at your code, the first group of messages tells you that there is no such file with a literal asterisk in its path. As you know, the asterisk is special to the shell; it expands to the matching file pathname components, if any. When you get that kind of error it can mean two things: the expansion failed because it can't match a real file; the expansion failed because it's disabled. So when you want to use an asterisk expansion (a "glob" technically) make sure to add "set +f" near the beginning of your script to enable this feature. If it fails you'll have to debug and figure out why the target filepath isn't recognized (is the file there? is the reference properly double quoted? and so on).

The message labelled "line 60" is telling you that something between paired "${" and "}" in that line looks wrong to the shell, which can't expand that something as you think it should. Try running your script with bash -x scriptname script-arguments and study its output. With patience you should be able to spot where the root cause is. Note: if the root cause should be found inside a "$(...)" construct, add set -x; immediately after "$(" to be able to study what really happens inside the "$(...)", that is, within the ellipsis.
User avatar
dr__Dan
Posts: 77
Joined: Tue Jul 28, 2020 5:06 am
Has thanked: 45 times
Been thanked: 26 times

Re: Fatdog Battery Applet 2

Post by dr__Dan »

Thanks step, I'll see what I find. I don't understand why there should be errors on lines I haven't changed from the working code, but that just means I get to learn something more.

Dan

9 years on with Fatdog64. :D

step
Posts: 512
Joined: Thu Aug 13, 2020 9:55 am
Has thanked: 50 times
Been thanked: 182 times
Contact:

Re: Fatdog Battery Applet 2

Post by step »

Welcome to POSIX-compliant shells! The reason why the glob isn't expanded is that dash doesn't expand globs in redirections. It kind of makes sense because a * glob can expand to multiple files whereas an input redirection can take a single file.

read value < /path/*
outputs "File not found: /path/*" in dash, ash but not in bash. Try it with dash and bash inside a directory where there's only one file.

How to get around this? The shell always expands globs in other contexts (the rules are detailed in the shell's man(ual) page). So, to get a glob positively expanded -- provided that the target file does exist -- you need to first expand the glob assigning the result to a variable, and then use that variable for the input redirection. Here's an idiom (one of many possible ones):

for expanded in /path/*; do break; done
read value < "$expanded"


The first line above also prevents the lingering issue of multiple files matching the glob. By breaking out of the for loop on its first iteration expanded is assigned the first globbed file name only. Note that $expanded = "/path/*" when there are no visible files or directories under /path.

Did you script originally run under the dash shell? Or was it either sh or bash? As I mentioned, bash is more lenient on expanding globs in a redirection. And in Fatdog64, sh is compiled to inherit several features available only in bash, so you could say that the default sh available in Fatdog64 is a superset of other default sh implementations available in other Linux systems. You don't need to concern yourself with such subtleties unless you write portable scripts, scripts that should run on multiple Linux distros, say Puppy, the Dogs, Debian-derivatives, etc.
User avatar
dr__Dan
Posts: 77
Joined: Tue Jul 28, 2020 5:06 am
Has thanked: 45 times
Been thanked: 26 times

Re: Fatdog Battery Applet 2

Post by dr__Dan »

Thank you step, you are more effective than the bash manual I have. The shell is dash because it was in the original. I should have some time this week to see about it.

Dan

9 years on with Fatdog64. :D

User avatar
dr__Dan
Posts: 77
Joined: Tue Jul 28, 2020 5:06 am
Has thanked: 45 times
Been thanked: 26 times

Re: Fatdog Battery Applet 2

Post by dr__Dan »

It's not August anymore, Toto. Strange times here in Oregon.

I've made the needed changes to restore, and also improve, functionality. Most notably, the icon coloration alerts the user as the battery reaches 20% charge and 80% charge, as these levels are purported to improve battery life.

I'd appreciate feedback and testing on various brands.

Also, if anyone can recommend a superior dash-specific tutorial, I'd gladly learn from it. It's slow going with just the specifications. I'm learning bash as well, and will likely use both as it seems appropriate, but the Arch wiki reports that dash is about four times faster and is lighter, and that fits the Fatdog concept.

Thanks for any input,

Dan

fatdog-battery-applet-2.2.sh.tar.gz
(2.86 KiB) Downloaded 42 times

9 years on with Fatdog64. :D

jamesbond
Posts: 561
Joined: Tue Aug 11, 2020 3:02 pm
Location: The Pale Blue Dot
Has thanked: 83 times
Been thanked: 300 times

Re: Fatdog Battery Applet 2

Post by jamesbond »

dr__Dan wrote: Fri Oct 09, 2020 3:30 am Also, if anyone can recommend a superior dash-specific tutorial
It's not tutorial, more like a reference, but I suppose better than nothing ... https://www.man7.org/linux/man-pages/man1/dash.1.html
User avatar
dr__Dan
Posts: 77
Joined: Tue Jul 28, 2020 5:06 am
Has thanked: 45 times
Been thanked: 26 times

Re: Fatdog Battery Applet 2

Post by dr__Dan »

Thank you, jamesbond, that's what I've been using. I've found limited additional information, but not much. Reading and re-reading the specification, and some trial and error, debugging, and experimental scripts has slowly drawn out the practical usage, but reviewing simple examples would speed it up.

9 years on with Fatdog64. :D

jamesbond
Posts: 561
Joined: Tue Aug 11, 2020 3:02 pm
Location: The Pale Blue Dot
Has thanked: 83 times
Been thanked: 300 times

Re: Fatdog Battery Applet 2

Post by jamesbond »

Ah, ok. Try this one instead: https://grymoire.com/Unix/Sh.html (if you wish you can also use the older "bourne shell" version of the same tutorial, also on the same site). I think the most important thing you need to note is that there are questionable practices that you need to avoid (as in, the results will be "undefined" and the behaviour will depend on which shell and which version you use ...)

Bash is posix-compliant and everything you learn from that site above is applicable when you use bash too.
In fact, I'd recommend that you stick to posix forms in your script (even when you use bash) so you know that it will work in different shells. Exception is, of course, if you're use "bash-only" features (arrays, virtual network devices, etc) and in this case you want to use the hashbang of #!/bin/bash instead of #!/bin/sh so you know you're using bash-specific feature.

When you're done, the same site offers tutorial for sed, awk, grep ... the most common tools you'll use in conjunction with shell scripting. And you'll be a wizard in no time :thumbup2:
user1111

Re: Fatdog Battery Applet 2

Post by user1111 »

In my Pugdog (a cut down Bulldog (which is the early cli (initram) stage of Fatdog))) I set the PS1 prompt to be spread across three lines that displays things such as time, date, current directory, internal IP, external IP, userid, host name ... and the battery level. Not generic as can vary from machine to machine (where the /sys/class/power_supply/BAT1/charge_full ... type battery details are recorded). For reference this is the top part of my /etc/profile that provides that ...

Code: Select all

export PATH=\
/usr/local/bin:\
/usr/bin:\
/usr/sbin:\
/bin:\
/sbin

# If running interactively, then:
if [ "$PS1" ]; then

	# External IP
	unset IP
	if [ -f /etc/ip ]
	then
		IP=`cat /etc/ip`
		IP="External IP : $IP "
	fi

	# Internal IP
	INTERNALIP=`/sbin/ifconfig wlan0 | grep -i mask | awk '{print $2}'| cut -f2 -d:`
	
	# Date
	D=`date +'%a %d %B %Y'`
	
	# Battery
	B_MAX=$(cat /sys/class/power_supply/BAT1/charge_full)
	B_NOW=$(cat /sys/class/power_supply/BAT1/charge_now)
	B_MAX=$((B_MAX / 100))      
	BATT=$((B_NOW / B_MAX))

	# Set prompt
	# too bright (in your face), changed to dark
	# export PS1='\n\[\e[33;1m\]\t \[\e[37m\]$D \[\e[32m\]$IP \[\e[30;1m\]Internal IP $INTERNALIP \[\e[34;1m\]\nPwd: \[\e[36m\]\w\n\[\e[35;1m\]\u\[\e[31m\]@\h> \[\e[0m\]'
	export PS1='\n\[\e[33;1m\]\t \[\e[30m\]$D \[\e[30m\]$IP \[\e[30;1m\]Internal IP $INTERNALIP\nBattery : $BATT \[\e[34;1m\]Pwd: \[\e[36m\]\w\n\[\e[35;1m\]\u\[\e[31m\]@\h> \[\e[0m\]'
.
.
.
Originally I did have it all in different bright colours, but that was too in-the-way, so I modified it to use black (30m escape colour code) which on my laptop still actually displays (different black to the black screen colour).

Booting into 1366x768 (laptop) resolution along with James' bbig font (setfont bbig) and as a old style 80x24 type cli screen - that looks good (IMO).

Sort of looks like this, but also with the external IP shown before the internal IP
p.png
p.png (15.16 KiB) Viewed 459 times

In Pugdog the black font colours are brighter than in that image (more comfortably visible).

Oh, the external IP for that is stored in /etc/ip, which is populated by a command (IIRC) run as part of wifi net connection ..
cd /etc; wget -q ipinfo.io/ip

Guess I should really change it to test for the battery being < 20% or whatever to have the prompt colour for the battery change to esc code 37m (white) .... and maybe even blinking :)
User avatar
dr__Dan
Posts: 77
Joined: Tue Jul 28, 2020 5:06 am
Has thanked: 45 times
Been thanked: 26 times

Re: Fatdog Battery Applet 2

Post by dr__Dan »

Thanks, rufwoof. I'll sort through your information, probably test it a bit. Do you mind telling what brand your computer is? Testing without access to some brands is one of two remaining hurdles for this project. :? Lines eg. 110-116 are an attempt to be more compatible.

Dan

9 years on with Fatdog64. :D

user1111

Re: Fatdog Battery Applet 2

Post by user1111 »

Hi Dan. Acer Aspire ES 15 (AMD E1 with Radeon R2 graphics)
Post Reply

Return to “Software”