LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-31-2021, 04:56 PM   #1
mjscott00
LQ Newbie
 
Registered: Aug 2011
Posts: 3

Rep: Reputation: Disabled
BASH script - array problem


I'm trying to write a script which captures all the available WiFi SSIDs in my area, then puts them into an array 'a'. I don't get an error, however, when I print the size of my array at the end of the script I get 0. I'm sure I've got my array syntax wrong, but cannot figure out what to change. Sample output is right after the code segment.
Thanks!


Code:
#!/bin/bash

a=()
n=0
sudo iwlist wlan0 scan |grep ESSID |grep -v '""' |sed 's/ESSID://' |sort |uniq |while read line; do
   echo "$n $line"
   a[$n]="$line"
   n=$((n+1))
   done
echo

echo ${#a[@]}
0 "NETGEAR03"
1 "Scott_Guest"
2 "Spyglass 2"
3 "TELUS0527"
4 "TELUS5D4C"
5 "wino911"

0
 
Old 05-31-2021, 05:46 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Looks very close.

Only suggestion is to enclose ${#a[@]} in double quotes.

I'd also echo a[n] each time you populate it to verify it's doing what you expect.

And enable bash debugging by adding:
Code:
set -vx
after the shebang line in your script. Regarding
 
Old 05-31-2021, 06:01 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,766

Rep: Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933
Code:
#!/bin/bash

a=() 
n=0
sudo iwlist wlan0 scan |grep ESSID |grep -v '""' |sed 's/ESSID://' |sort |uniq |while read line; do
   echo "$n $line"
   a[$n]="$line"  
   n=$((n+1))
   done
echo

echo ${#a[@]}
When you pipe results to a loop, it runs in a subshell so when it exits so does your array.
Untested..

Code:
a=()
while read line
do
  # perform computations on $line
  a+=($line)
done < <( sudo iwlist wlan0 scan |grep ESSID |grep -v '""' |sed 's/ESSID://' |sort |uniq  )
You can check syntax errors at https://www.shellcheck.net/

Last edited by michaelk; 06-01-2021 at 03:54 AM.
 
1 members found this post helpful.
Old 05-31-2021, 10:57 PM   #4
mjscott00
LQ Newbie
 
Registered: Aug 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you Michaelk - this was exactly the issue!!!
 
Old 06-01-2021, 07:26 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,766

Rep: Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933
In addition bash 4+ also has the mapfile function.

mapfile -t a < <( sudo iwlist wlan0 scan |grep ESSID |grep -v '""' |sed 's/ESSID://' |sort |uniq )
 
Old 06-01-2021, 11:53 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,996

Rep: Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338
Code:
sudo iwlist wlan0 scan | awk -F\" '/ESSID:/ { print $2 } '
# and if you wish to sort just add: 
awk ...    | sort -u 
# uniq is not required
But we do not need awk too:
Code:
#!/bin/bash
i=0
while read -r line
do
     L=( ${line//\"/ } )
     [[ ${L[0]} == ESSID: ]] || continue
     a[$i]="${L[1]}"
     (( i++ ))
done < <(sudo iwlist wlan scan)
printf "%s\n" "${a[@]}"
 
Old 06-02-2021, 02:43 AM   #7
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,378

Rep: Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757
If you can live without the sort
Code:
#!/bin/bash

while read line; do
  [[ "$line" =~ ESSID:\"(.+)\" ]] && echo ${BASH_REMATCH[1]}
done < <(sudo iwlist wlan0 scan)

Last edited by allend; 06-02-2021 at 02:44 AM.
 
Old 06-02-2021, 06:41 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,818

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
If you have a one-character delimiter then consider IFS
Code:
i=0
while IFS=\" read -r key val junk
do
    case $key in
    ( ESSID: )
     a[i++]=$val
    esac
done < <(sudo iwlist wlan scan)
printf "%s\n" "${a[@]}"
 
Old 06-02-2021, 03:21 PM   #9
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
It may be you are doing it wrong.
On my system I get
[/code]
$ sudo iwlist wlp4s0 scan
wlp4s0 Interface doesn't support scanning.
[/code]

Try just the basic command first to see if you get expected results then add to it until you see the expected output before you put it into the loop

Last edited by computersavvy; 06-02-2021 at 03:32 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 04:44 AM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-02-2009 11:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration