loops - Populating Bash array fails -
here simple command running in bash, array won;t populated reason.
array=() && sudo iwlist wlan0 scan | grep 'address'| while read line; array[${#array[@]}]=$line; done
i have tried populate array way:
array=() sudo iwlist wlan0 scan | grep 'address'| while read line; array+=($line); done
but gives me same result. know works because when this:
sudo iwlist wlan0 scan | grep 'address'| while read line; "echo $line"; done
it print every line piped grep while loop.
when check size of array " echo ${#array[@] " show 0 , if print array prints nothing. see errors in line?
**update. got working using loop so:
for line in $(sudo iwlist wlan0 scan | grep 'address' -a5); array+=($line); done
bash faq entry #24: "i set variables in loop that's in pipeline. why disappear after loop terminates? or, why can't pipe data read?"
the while loop [...] executed in new subshell own copy of variable [...]
use following workaround:
while read line; array+=("$line") done < <(sudo iwlist wlan0 scan | grep 'address')
Comments
Post a Comment