Reputation: 2797
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?
Upvotes: 0
Views: 305
Reputation: 125838
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
Upvotes: 1