2

I have a script:

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

Why my condition

[[ "$ENABLE_SYSLOG" = true ]] && logger -t inotifywait -p user.info "$CHMOD_LOG"

Not work ?

If I remove

[[ "$ENABLE_SYSLOG" = true ]] &&

Then everything is fine.

I try to remove ampersand (&), try other condition (as you can see in example), try if instead of [[ - but all in vain.

Any condition is not work

Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
vskubriev
  • 826
  • 1
  • 11
  • 21
  • You can just say: `"$ENABLE_SYSLOG" && ...` – anubhava Mar 28 '14 at 07:49
  • 1
    While this may seem clever, it's probably not a good idea. You should try to find out why your attempt does not work, since it should. What version of `bash` are you using? How do you run the script? – Adrian Frühwirth Mar 28 '14 at 08:01
  • I ran a script via upstart job(script ... end script) in ubuntu 12.04 (bash version 4.2.25(1)). May be upstart does not use bash(use dash or sh instead)? – vskubriev Mar 28 '14 at 08:05
  • if your problem is solved, please, either delete the question or post an answer and accept it after 48 hrs. Good luck to all. – shellter Mar 28 '14 at 11:21
  • 3
    Ubuntu indeed uses `dash`, not `bash`, for system scripts, so `[[` is not supported. – chepner Mar 28 '14 at 12:47

1 Answers1

2

[[ ... ]] is present in most advanced shells (ksh, bash, and zsh) but is missing in POSIX sh and in Ubuntu /bin/sh (which is a symlink to /bin/dash).

You can either replace [[ ... ]] with [ ... ] or test ... (both are equivalent, the second makes it clear that test is a command while [ ... ] appears as if it is special syntax), or (unless the script is explicitly invoked as sh FILENAME), start it with #!/bin/bash. Note that [[ ... ]] is parsed differently than test or [ ... ], as [[ is a reserved word, while [ and test are merely builtin commands. Specifically, field splitting and globbing are not done between [[ and ]], while = and == in this context do shell pattern matching if neither string is quoted. Furthermore, the =~ operator to do regexp matching is not present in test or [.

Demi
  • 3,535
  • 5
  • 29
  • 45