我有一个脚本:
ENABLE_SYSLOG=true
test -r /etc/default/inotifywait && . /etc/default/inotifywait || exit 99
test -d $INOTIFY_FOLDER || exit 100
inotifywait -mrq -e ATTRIB --format '%w%f' "$INOTIFY_FOLDER" | while IFS= read -r FILE
do
if [ -f $FILE ];then
# If file
if [ `stat -c %a $FILE` != "664" ] ;then
CHMOD_LOG=$(chmod -v 664 "$FILE")
[[ "$ENABLE_SYSLOG" = true ]] && logger -t inotifywait -p user.info "$CHMOD_LOG" &
fi
else
# If directory
if [ `stat -c %a $FILE` != "2775" ] ;then
CHMOD_LOG=$(chmod -v 2775 "$FILE")
[[ "$ENABLE_SYSLOG" = true ]] && logger -t inotifywait -p user.info "$CHMOD_LOG" &
fi
fi
done
为什么我的情况
[[ "$ENABLE_SYSLOG" = true ]] && logger -t inotifywait -p user.info "$CHMOD_LOG"
不工作?
如果我删除
[[ "$ENABLE_SYSLOG" = true ]] &&
那么一切都很好。
我尝试删除与符号(&),尝试其他条件(如您在示例中看到的),尝试 if 而不是 [[ - 但一切都是徒劳的。
任何条件都不行
最佳答案
[[ ... ]]
出现在最高级的 shell 中(ksh
、bash
和 zsh
>) 但在 POSIX sh
和 Ubuntu /bin/sh
中缺失(这是 /bin/dash
的符号链接(symbolic link))。
您可以将 [[ ... ]]
替换为 [ ... ]
或 test ...
(两者是等效的,第二个清楚地表明 test
是一个命令,而 [ ... ]
看起来好像是特殊语法),或者(除非脚本被显式调用为 sh FILENAME
),以 #!/bin/bash
启动。请注意,[[ ... ]]
的解析方式与 test
或 [ ... ]
不同,如 [[
是保留字,而 [
和 test
只是内置命令。具体来说,[[
和 ]]
之间没有进行字段分割和通配,而 =
和 ==
如果两个字符串都没有被引用,则此上下文执行 shell 模式匹配。此外,用于进行正则表达式匹配的 =~
运算符在 test
或 [
中不存在。
关于upstart - shell脚本: `[[` not working as expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22706978/