Bash 脚本未按预期运行

Bash 脚本未按预期运行

我正在尝试获取下面的脚本以获取传递给它的连接 uuid(由网络管理器),将其与我的家庭连接(物理连接和 VPN)的 uuid 列表进行比较,并运行某些命令。然而,即使 uuid 是正确的,脚本似乎总是转到“else”子句。

#!/bin/bash

echo $CONNECTION_UUID

Home_UUIDs="5b509e73-8f08-475c-a14d-7037f2f4b87a ba36c69e-c3f1-4480-a484-1eea105311b2 f3889052-719f-4b88-8b94-e1fa189d44f9"

if [[ "$Home_UUIDs" == *"$CONNECTION_UUID"* ]]; then
        case "$2" in
                up)
                echo "Conecting to Home netork..."
                ntpdate -vs 192.168.0.18
                service ntp start
                ;;
                down)
                echo "Disconnecting from home network"
                service ntp stop
                ;;
        esac
else
        echo "We're not at home..."
        service ntp stop
fi

exit 0

答案1

它似乎对我来说适用于所有三个 uuid:

#!/bin/bash
Home_UUIDs='5b509e73-8f08-475c-a14d-7037f2f4b87a ba36c69e-c3f1-4480-a484-1eea105311b2 f3889052-719f-4b88-8b94-e1fa189d44f9'

for CONNECTION_UUID in 5b509e73-8f08-475c-a14d-7037f2f4b87a \
                       ba36c69e-c3f1-4480-a484-1eea105311b2 \
                       f3889052-719f-4b88-8b94-e1fa189d44f9 ; do

    if [[ "$Home_UUIDs" == *"$CONNECTION_UUID"* ]]; then
        echo "We're at home!"
    else
        echo "We're not at home..."
    fi
done

$CONNECTION_UUID 中还有其他字符吗?

答案2

最后通过将括号中的语句替换为来修复它

[[ "$Home_UUIDs" =~ "$CONNECTION_UUID" ]]

代替

[[ "$Home_UUIDs" == *"$CONNECTION_UUID"* ]]

相关内容