我試圖獲取下面的腳本來獲取傳遞給它的連接 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"* ]]