Shell Script Arrays - Running cat
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 The output this is giving me is this: Code:
present: Code:
present: yes 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. |
i've not done this with arrays in bash directly, but i'm sure you need to look at the $IFS vairable. by default IFS (Input Field Seperateor) is white space, so to delimit the output you have, everything seperated by some form of white space is a different record. instead, you'd want to change $IFS to be a new line temporarily. try this:
IFS='\n' battStats=( $(cat $BATT_DIR/$batt/state) ) |
Doing that is actually giving me a syntax error:
Code:
line 60: 0 BTW... line 60 is the echo statement in this snippet: Code:
for battElement in $(seq 0 $((${#battStats[@]} - 1))) |
In another thread I've found that there should be
Code:
IFS=$'\n' |
HUZZAH!!
Thank you SO much! That works perfectly. Now to just finish up the rest of my script. |
All times are GMT -5. The time now is 08:54 AM. |