I wanted to write a shell script that utilized a non-acpi-directory command line interface to the battery charge of my laptop (I won't bother to go into the reasons). Everything was doing just fine until I attempted to run a cat on the /proc/acpi/battery/BATx/state information and store it in an array.
The following code is what I am using for my "test"
Code:
# load the list of avaialble batteries
dirList=( $(ls -1 $BATT_DIR) )
# make sure we have data in the array
if [ ${#dirList[@]} -lt 1 ]; then
echo "No batteries located (I looked in $BATT_DIR)!"
exit 3
fi
# now, load the data for each battery
for element in $(seq 0 $((${#dirList[@]} - 1)))
do
# data load
batt=${dirList[$element]}
battStats=( $(cat $BATT_DIR/$batt/state) )
for battElement in $(seq 0 $((${#battStats[@]} - 1)))
do
echo ${battStats[$battElement]}
done
done
The data actually winds up in the array, but not in the format I was expecting it to. I was expecting my use of cat to place each line of the cat into the array as an individual element, not each row and column.
The output this is giving me is this:
Code:
present:
yes
capacity
state:
ok
charging
state:
charging
present
rate:
unknown
remaining
capacity:
5760
mAh
present
voltage:
unknown
This output I am expecting is this:
Code:
present: yes
capacity state: ok
charging state: charging
present rate: unknown
remaining capacity: 5760 mAh
present voltage: unknown
While the output I have is an acceptable "
hack" for utilization in my script, it doesn't really provide what I had hoped. I wanted to parse the text in case the data in that file changes in future acpi/kernel releases instead of just relying on hard-coded indexes utilizing the colon character as my "
end of label" indicator.
This concerns me because I also want to cat the /proc/acpi/battery/BATx/info file for the "
design capacity" in order to calculate the percentage of battery life remaining. If either changes, my shell script will wind up vomiting all over me.
I wanted to avoid doing this in C/C++, even though it fits the solution as well. But I may wind up switching to that if I can't get this working.
Thanks.