13 ms·
So this just reads /sys/class/power_supply/BAT0/energy_now (or charge_now) and /sys/class/power_supply/BAT0/charge_full and outputs it nicely. While the asm is
by Kluggy 3y ago
So this just reads /sys/class/power_supply/BAT0/energy_now (or charge_now) and /sys/class/power_supply/BAT0/charge_full and outputs it nicely. While the asm is cool, how large would the bash script to do it?
- meribold 3y agoThis project started as a rewrite of a Bash script (for learning purposes), so I can easily answer this question. It's indeed small. Here's the full Bash script I used to use (including comments): #!/usr/bin/env bash # Print the remaining amount of energy (or charge) # stored in the battery identified by `BAT0`. This # is what works on my ThinkPad X220 and may not be # portable to other laptops without changes. cd /sys/class/power_supply/BAT0 || exit $? # Sometimes there are `energy_now` and `energy_full` # pseudofiles, and sometimes there are `charge_now` # and `charge_full` pseudofiles instead. if [[ -e energy_now ]]; then now=$(<energy_now) full=$(<energy_full) unit=Wh elif [[ -e charge_now ]]; then now=$(<charge_now) full=$(<charge_full) unit=Ah fi percent=$((100 * now / full)) # Convert from microwatt-hours (or microampere-hours) # to watt-hours (or ampere-hours). now=$(bc <<< "scale=1; $now / 1000000") full=$(bc <<< "scale=1; $full / 1000000") echo "$now $unit / $full $unit (${percent}%)"