我正在尝试使用nmcli使用 bash 文件在已填充的网络之间进行切换,但我得到了无意义的数据。
不过废话少说
1) 0
#? 0
代码如下。
#!/bin/bash
# Pre-populated UUID data for network connections
networks=(
["tr_5"]="127f3e9e-34dd-444e-aee4-7c2b35c7c307"
["VC_HotSpot_6"]="c498aa6a-24d4-4b51-92c7-9b8d84181fc1"
["Network_3"]="34567890-3456-3456-3456-345678901234"
)
# List available network connections
echo "Available network connections:"
nmcli connection show
# Prompt the user to choose a network
echo "Select the network to connect to:"
select network_name in "${!networks[@]}"; do
if [[ -n $network_name ]]; then
break
fi
done
# Get the UUID of the selected network
network_uuid=${networks[$network_name]}
# Check if the network UUID exists
if [[ -z $network_uuid ]]; then
echo "Network '$network_name' UUID not found. Exiting..."
exit 1
fi
# Disconnect from the current network (if connected)
current_connection=$(nmcli connection show --active | awk 'NR>1{print $1}')
if [[ -n $current_connection ]]; then
echo "Disconnecting from the current network..."
nmcli connection down $current_connection
fi
# Connect to the chosen network
echo "Connecting to network '$network_name'..."
nmcli connection up uuid $network_uuid
# Display the connection status
echo "Connection status:"
nmcli connection show --active | grep -E "($network_name|$current_connection)"
echo "Network switch completed successfully!"
我运行脚本 ./network_switch.sh
不过废话少说
1) 0
#? 0
declare
在这种情况下,使用内置函数检查赋值结果会很有帮助:您将看到您已经创建了一个带有单个第 0 个元素的索引数组 ( )。
-a
这是因为该name=(...)
语法默认创建索引数组,并且它只是将字符串值“键”计算为数字 0 并连续覆盖该值。要创建关联数组,您需要显式声明它,或者
或者
然后您可以检查
declare -p
: