upstart - shell脚本: `[[` not working as expected

标签 upstart dash-shell

我有一个脚本:

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 中(kshbashzsh >) 但在 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/

相关文章:

process - 使用Upstart设置内存消耗限制

bash - 如何将 shell 从 bash 更改为 dash

linux - 如何在 Amazon Linux 上升级/安装 Upstart deamon 版本 1.5

java - Upstart 在另一项工作开始并正在运行后延迟开始工作

linux - 在 Linux 和 BSD 中使用和不使用 shebang 执行 Bash 脚本

linux - Dash,文件路径是否有任何限制

linux - 在 SIGINT 上杀死后台进程

shell - 两个字符串中包含的字符 - 已编辑

linux - ubuntu:启动( Upstart )mongodb的第二个实例

ruby - 我应该将非 Ruby 文件放在我的 gem 中的什么位置?