Friday, 23 August 2013

Populating Bash array fails

Populating Bash array fails

Here is a simple command I am running in bash, but the array won;t get
populated for some reason.
array=() && sudo iwlist wlan0 scan | grep 'Address'| while read line; do
array[${#array[@]}]=$line; done
I have also tried to populate the array this way:
array=()
sudo iwlist wlan0 scan | grep 'Address'| while read line; do
array+=($line); done
but it gives me the same result. I know it works because when i do this:
sudo iwlist wlan0 scan | grep 'Address'| while read line; do "echo $line";
done
it will print every line that is piped from grep to the while loop.
When I check the size of the array " echo ${#array[@] " it will show 0 and
if I print the array it obviously prints nothing. Do you see any errors in
the line?

No comments:

Post a Comment