LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Facing a problem because of the "Spaces" in networks names (https://www.linuxquestions.org/questions/linux-networking-3/facing-a-problem-because-of-the-spaces-in-networks-names-4175735471/)

1krdk 03-29-2024 05:27 PM

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!

teckk 03-29-2024 06:00 PM

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.

michaelk 03-29-2024 06:57 PM

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.

syg00 03-29-2024 08:04 PM

nm :eek:


All times are GMT -5. The time now is 10:15 PM.