LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2024, 05:27 PM   #1
1krdk
LQ Newbie
 
Registered: Mar 2024
Posts: 1

Rep: Reputation: 0
Facing a problem because of the "Spaces" in networks names


Hello everyone!
i have a thin client device with armbian cli OS, and I've created a script that would "using nmcli" list a numerated list of the names of the available networks and select one of them by it's number like:
Available networks:
1. my_home
2. some_network
3. my
4. wifi
"the problem is in listing network 3,4 in different lines"
when clicking on 1, it would ask for the password of "my_home",then type it's password and you are connected..
the problem is if i want to connect to "my wifi" network it is seeing it as if it's a different netowkrs because of the space between my and wifi in the name

here is the functions i'm using:
Code:
connect_network() {
        local networks=( $(nmcli -f SSID device wifi list | awk 'NR>1{print NR-1".", $0}') )
        if [ ${#networks[@]} -eq 0 ]; then
        echo "No available networks found. Exiting."
        exit 1
        fi

        while true; do
        echo "Available networks:"
        for (( i=0; i<${#networks[@]}; i++ )); do
                echo "$((i+1)). ${networks[$i]}"
        done
when the network name contains a space, I'm unsure how to properly format it for connection because nmcli uses my\ wifi when there is a spaces

Could someone please provide me with a solution or workaround for connecting to the Wi-Fi networks with spaces in their names within the script? Any assistance would be greatly appreciated.

Thank you!
 
Old 03-29-2024, 06:00 PM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,138
Blog Entries: 6

Rep: Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827
I don't have network manager.

Yours
Code:
networks=(my_home some_network my wifi)

echo "Available networks:"
for (( i=0; i<${#networks[@]}; i++ )); do
        echo "$((i+1)). ${networks[$i]}"
done
You need it like this.
Code:
networks=('my_home' 'some_network' 'my wifi')

echo "Available networks:"
for (( i=0; i<${#networks[@]}; i++ )); do
        echo "$((i+1)). ${networks[$i]}"
done
That may be vague, but you need to spit out your array with the names quoted.
 
2 members found this post helpful.
Old 03-29-2024, 06:57 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,710

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
As posted...
Quote:
awk 'NR>1{print NR-1".", $0}')
This would output 1. SSID

And therefore
Quote:
echo "$((i+1)). ${networks[$i]}"
would output:
Quote:
1. 1.
2. SSID #1
As posted the elements of the array are split on spaces so that is why there are separate lines. I am sure there is a better way but for a simple workaround you can try:
Code:
res=$(nmcli -f SSID device wifi list | awk 'NR>1{print $0}')
readarray -t networks <<< "$res"
readarray splits on end line character not spaces.

If I "read" into your script you are using the second loop to create a menu? Check out bash's select, it might make your script simpler.

Last edited by michaelk; 03-29-2024 at 08:09 PM.
 
2 members found this post helpful.
Old 03-29-2024, 08:04 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,129

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
nm
 
2 members found this post helpful.
  


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
network interface names having strange names because of udev rules pascaltaf Linux - Kernel 5 01-08-2015 09:07 AM
[SOLVED] I want to extract names with spaces from a list of names! linustalman Linux - General 5 12-02-2011 09:42 AM
Spaces and escaped spaces pslacerda Linux - Newbie 13 12-20-2008 09:03 AM
Help With Java Problem Please"""""""""""" suemcholan Linux - Newbie 1 04-02-2008 06:02 PM
how to get rid of " " spaces in file names in a directory lleb Linux - General 7 08-09-2006 04:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 03:06 PM.

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