1

I'm trying to get the script below to take the connection uuid passed to it (by networkmanager), compare it against a list of uuids for my home connections (both physical and VPN) and run certain commands. However, the script appears to go to the "else" clause all the time, even though the uuid is correct.

#!/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
  • first of all...where is the value of $CONNECTION_UUID coming from? I don't see it being set. – dave_alcarin Nov 3 '15 at 13:41
  • 1
    I don't understand why you put stars (*) around $CONNECTION_UUID, can you explain? thx – lese Nov 3 '15 at 13:41
  • @dave_alcarin It's set by NetworkManager when is is run, along with the value of $2. I've already checked that the UUID is passed correctly. – tmac1 Nov 3 '15 at 13:43
  • 1
    You are comparing a string against a list of strings. The string is never going to be equal to the list, even if it is part of it. You want to check if your UUID is in the list, not if it is equal. – dave_alcarin Nov 3 '15 at 13:45
  • 2
    @dave_alcarin I thought the asterisks around it did that. – tmac1 Nov 3 '15 at 13:46
1

It seems to work for me for all three uuids:

#!/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

Are there any other characters in $CONNECTION_UUID?

| improve this answer | |
0

Finally fixed it by replacing the statement in brackets with

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

instead of

[[ "$Home_UUIDs" == *"$CONNECTION_UUID"* ]]
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.