0

Hello I have the following script:

#! /bin/bash
 Output=$(defaults read com.apple.systemuiserver menuExtras | grep Bluetooth.menu)
Check="\"/System/Library/CoreServices/Menu Extras/Bluetooth.menu\","
echo $Output
echo $Check
     if [ "$Output" = "$Check" ]
    then
           echo "OK"
else
    echo "FALSE" 
    echo "Security Compliance Setting 'Show Bluetooth Status in Menu Bar(2.1.3)' has been changed!" | logger

fi

When you run it, both variables have the exact same output however the check always says it is FALSE

here is the out put from my terminal:

"/System/Library/CoreServices/Menu Extras/Bluetooth.menu",
"/System/Library/CoreServices/Menu Extras/Bluetooth.menu",
FALSE

Any idea why it is not detecting that they are the same?

4
  • 3
    Use echo "[$Output]" and echo "[$Check]" Commented Feb 12, 2015 at 19:23
  • 1
    Or declare -p Output Check. Commented Feb 12, 2015 at 19:23
  • Or printf '%q\n' "$Output" "$Check". Commented Feb 12, 2015 at 19:26
  • 4
    I suspect that Output contains leading spaces, that can't be seen by OP with unquoted (horror!) echo $Output, so OP is very confused. OP should learn how to properly use quotes. But I'm just assuming. OP should learn how to properly use quotes anyway. Commented Feb 12, 2015 at 19:32

1 Answer 1

1

As everyone in the comments suspected, the problem is whitespace in $Output (which echo $Output removes); specifically, 4 leading spaces (note that in the following, "$ " is my shell prompt):

$ defaults read com.apple.systemuiserver menuExtras | grep Bluetooth.menu
    "/System/Library/CoreServices/Menu Extras/Bluetooth.menu",
$ Output=$(defaults read com.apple.systemuiserver menuExtras | grep Bluetooth.menu)
$ echo $Output
"/System/Library/CoreServices/Menu Extras/Bluetooth.menu",
$ echo "[$Output]"
[    "/System/Library/CoreServices/Menu Extras/Bluetooth.menu",]
$ Check="    \"/System/Library/CoreServices/Menu Extras/Bluetooth.menu\","
$ if [ "$Output" = "$Check" ]; then echo "OK"; else echo "FALSE"; fi
OK

Note that since the number of spaces might not always be the same, it might be safer to use bash's wildcard matching capability within a [[ ]] conditional expression (this will not work with [ ]):

$ Check="\"/System/Library/CoreServices/Menu Extras/Bluetooth.menu\","
$ if [[ "$Output" = *"$Check" ]]; then echo "OK"; else echo "FALSE"; fi
OK

You could also skip the string comparison entirely, and just use the fact that grep returns a success status only if it finds a match:

#!/bin/bash
if defaults read com.apple.systemuiserver menuExtras | grep -q "/System/Library/CoreServices/Menu Extras/Bluetooth.menu"; then
    echo "OK"
else
    echo "FALSE" 
    echo "Security Compliance Setting 'Show Bluetooth Status in Menu Bar(2.1.3)' has been changed!" | logger
fi
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for you amazing answer Gordon! It is much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.