$ cat /proc/asound/cards
0 [MID ]: HDA-Intel - HDA Intel MID
HDA Intel MID at 0xb0610000 irq 64
1 [PCH ]: HDA-Intel - HDA Intel PCH
HDA Intel PCH at 0xb0614000 irq 65
I am trying to save the second line of each entry into a BASH array:
eval sound_card_array=($(awk 'NR%2==0' /proc/asound/cards)) #also tried without eval -> same result
#echo $(awk 'NR%2==0' /proc/asound/cards) --> this produces the proper output of each line
which should look like this:
["HDA Intel MID at 0xb0610000 irq 64", "HDA Intel PCH at 0xb0614000 irq 65"]
Instead, when I print out sound_card_array, I get only parts of the strings, or I get them all in different array elements. I have tried putting quotes around the awk call, adding echo, putting quotes around the variable when I access the array (i.e. "${sound_card_array[$i]}"), but none work.
UPDATE: Thanks guys. Maybe my problem is also with the way I'm accessing the array:
for sc in "${!sound_card_array[@]}"
do
if [ "$sc" -lt "$((${#sound_card_array[@]}-1))" ]; then
j_pair $sc "${sound_card_array[$i]}"
else
j_pair_last $sc "${sound_card_array[$i]}"
fi
done
where j_pair and j_pair_last are BASH functions I wrote which take two arguments and echo them to a file:
function j_pair {
printTabs
echo "\"$1\":\"$2\","
}
function j_pair_last {
printTabs
echo "\"$1\":\"$2\""
}
I'm new to BASH, so I'm sure there are many things I could be doing wrong. Thanks again for all the help!
For awk and printf:
$ arr=($(awk 'NR%2==0{$1=$1; print}' /proc/asound/cards))
$ printf "%s\n" "${arr[@]}"
HDA
Intel
MID
at
0xb0610000
irq
64
HDA
Intel
PCH
at
0xb0614000
irq
65