You are not logged in.

#1 2015-07-31 23:23:15

Nycroth
Member
Registered: 2014-04-16
Posts: 7

Sourcing /sys/class/power_supply Outputs

I'm writing a script that needs to utilize only the files in /sys/class/power_supply to produce a battery charge percent and time remaining. Unfortunately, I do not have a laptop.

Anyone who can help me by dumping the output of every file in there would greatly help me. Please use Paste Bin.

Below is a script that will produce exactly what I need. Though feel free to write your own if you don't trust me.

#!/bin/zsh

# Needed to utilize the **(.) operator
setopt extended_glob

FILENAME="power_output.txt"

# Prevent Overwrite
[[ -e "$FILENAME" ]] && exit 1

# Start with a tree output (if tree is installed)
[[ -e /bin/tree ]] && tree -a /sys/class/power_supply >> $FILENAME

for file in "/sys/class/power_supply/**(.)"; do
    echo "\n# $file:" >> $FILENAME
    cat $file >> $FILENAME
done

Thank you so so much in advance!

EDIT: Thank's @jasonwryan for catching the errors in the script! big_smile

Last edited by Nycroth (2015-08-01 00:13:45)

Offline

#2 2015-07-31 23:54:00

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Sourcing /sys/class/power_supply Outputs

There are a couple of issues with your script.

First, you need to quote /sys/class/power_supply/**(.) or the glob will not expand.

Second, where is $i set? You probably want "$file" (again, quoted) there...

Output:

/sys/class/power_supply
├── ADP1 -> ../../devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0003:00/power_supply/ADP1
└── BAT1 -> ../../devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:01/PNP0C09:00/PNP0C0A:00/power_supply/BAT1

2 directories, 0 files

# edit: I use this (simpler, but does what you seem to be asking)

#!/usr/bin/env bash
# battery status script

batt=/sys/class/power_supply/BAT1/

current=$(<"${batt}"/charge_now)
full=$(<"${batt}"/charge_full)
charge=$(( current * 100 / full )) 

printf '%s\n' "${charge}%"

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2015-08-01 00:20:00

Nycroth
Member
Registered: 2014-04-16
Posts: 7

Re: Sourcing /sys/class/power_supply Outputs

Thank you @jasonwryan, for catching those errors. Servers me right for last second untested changes.

Your script is a great start but I'd also like to support a time remaining until discharged. I'm just not sure what files I could use to accomplish that.

There are also reports on the forums that booting with power and booting without power change both the file names and possibly contained values from change_* to energy_* and reports amps (current/charge) or watts (energy) (Source: https://bbs.archlinux.org/viewtopic.php?id=131025).

Last edited by Nycroth (2015-08-01 00:23:41)

Offline

#4 2015-08-01 00:25:23

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Sourcing /sys/class/power_supply Outputs

Nycroth wrote:

Your script is a great start but I'd also like to support a time remaining until discharged. I'm just not sure what files I could use to accomplish that.

Me neither. It would be a pretty ropey estimation at best anyway as it could only be predicated on current resource usage.

There are also reports on the forums that booting with power and booting without power change both the file names and possibly contained values from change_* to energy_* (I can't recall the value change) (Source: https://bbs.archlinux.org/viewtopic.php?id=131025).

Not on my laptops, but it may be a firmware thing...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2015-08-01 01:04:07

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,159
Website

Re: Sourcing /sys/class/power_supply Outputs

Is there any reason you can't use acpi?  Why try to reinvent the wheel when what you are looking for is exactly what it already does?


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#6 2015-08-01 01:09:48

Nycroth
Member
Registered: 2014-04-16
Posts: 7

Re: Sourcing /sys/class/power_supply Outputs

@Trilby: dependency issue. i3block's maintainer wants basically no dependencies in core scripts of the project. Previous script was written in Perl and relied on ACPI.

Last edited by Nycroth (2015-08-01 01:37:57)

Offline

Board footer

Powered by FluxBB