0

The following code gives me an error when I run it, I can't see where I have went wrong.

I have looked up man as well.

while [inotifywait -m -r -e modify -e move -e create -e delete '/home/client/Files' ]; do
    echo "File Notification" | mail -s "Client File Notification" -t "[email protected]"
done

3 Answers 3

3

[ is a command that do a test. You did not want to test something about inotify, but want to test the result of that command. so bracket [ ... ] is too much. You have to write:

while inotifiwait ... ;do

or better

while array_answer=($(inotifywait ...)); [ "$array_answer" ] ;do
    echo Event is: ${array_answer[0]}
    echo file is: ${array_answer[1]}
    ...
    done
Sign up to request clarification or add additional context in comments.

1 Comment

Good point; I saw the missing space after the ' ', but didn't notice that the command shouldn't be between square brackets in the first place.
2

[ is actually a command name, a synonym for test, so you need whitespace before and after it.

Change this:

while [inotifywait ... ]; do

to this:

while [ inotifywait ... ]; do

Without the space, the shell will probably try to execute a command called [inotifywait, which presumably doesn't exist. (You should have shown us the error message in the body of the question; I didn't initially notice that you quoted it in the title.)

(Note that it's ok for the ; to immediately follow the ], since ; is a shell metacharacter.)

1 Comment

And as @F.Hauri's answer correctly points out, the inotifywait command shouldn't be between square brackets in the first place.
1

While F. Hauri is right about the mistake in the while condition, that doesn't explain the "error near unexpected token 'done'" error. There's nothing in the commands you posted that'd cause this error, so I suspect you may have invisible characters in the file, possibly carriage returns at the end of the lines (they're the norm for Windows/DOS text files). Try viewing the script with cat -v and look for "^M" at the end of lines, or anything else weird.

If the problem is errant carriage returns, use dos2unix to remove them, then switch to a text editor that doesn't add them in the first place.

2 Comments

That's a common problem under Cygwin. Many Cygwin commands can handle DOS-style line endings, but the shell doesn't; it would treat done^M as a command name, where ^M is an ASCII carriage return character. A copy-and-paste of the error message would be helpful. Note that dos2unix replaces its input file, unlike most Unix text filter commands.
Thank's for all you're help, I am new to shell scripts so I didn't know about the [] bracket test case. The problem that was causing my error was I was using Notepad in Windows mode, I switched it to unix mode and it fixed the problem. Thanks Again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.