0

I'm ver unfamiliar with writing shell scripts but I am trying to write a script that checks whether a file is currently open before executing a command. I'm getting the following error

 ./Script.sh: line 9: syntax error near unexpected token `done'
./Script.sh: line 9: `done'

As a bonus I don't know that the "|" operator does I've found a few shell syntax sites but since its a single character searching for its purpose has been difficult.

#!/bin/bash

inotifywait -m -r -e create "sunshine" | while read NEWFILE
do
        if [ lsof | grep NEWFILE ]; then
                echo "found something";
        else
                aws s3 sync sunshine s3://turnaround-sunshine/
done
Blue
  • 1,408
  • 4
  • 17
  • 36
  • Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Jun 22 '18 at 04:22

1 Answers1

2

Your if/else is missing a fi at the end.

| creates a pipe between two commands with the output of the first fed as input to the second. The second pipe in the if statement should not have square brackets, by the way; and grep NEWFILE should be grep "$NEWFILE":

if lsof | grep "$NEWFILE"; then

Use ShellCheck to catch these types of errors.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578