您好我有以下脚本:
#! /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
当你运行它时,两个变量都具有完全相同的输出,但是检查总是说它是FALSE
这是我终端的输出:
"/System/Library/CoreServices/Menu Extras/Bluetooth.menu",
"/System/Library/CoreServices/Menu Extras/Bluetooth.menu",
FALSE
知道为什么它没有发现它们是一样的吗?
答案 0 :(得分:1)
正如评论中的每个人都怀疑的那样,问题是$ Output中的空格(echo $Output
删除);具体来说,4个前导空格(请注意,在下面,“$”是我的shell提示符):
$ 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
请注意,由于空格的数量可能并不总是相同,因此在[[ ]]
条件表达式中使用bash的通配符匹配功能可能更安全(此将无效与{ {1}}):
[ ]
您也可以完全跳过字符串比较,只使用$ Check="\"/System/Library/CoreServices/Menu Extras/Bluetooth.menu\","
$ if [[ "$Output" = *"$Check" ]]; then echo "OK"; else echo "FALSE"; fi
OK
仅在找到匹配项时才返回成功状态的事实:
grep