Adam Jones
Adam Jones

Reputation: 43

Error with fi in if then else shell script

There is a shell script with if, then, else. Here is the part of the code, it's not the whole, just the part:

DAYOFWEEK=$(date +"%u")
echo $DAYOFWEEK
if [ "$DAYOFWEEK" -eq 1 ]; then
echo "OK. It's Monday. We are running a weekly backup on Mondays."

echo "`date` - Deleting weekly remote backup files."
sftp -oPort=199 $SFTPUSER@$SFTPSITE <<EOF;
    cd user;
    cd weekly;
    ls -al;
    rm *;
    bye;
    EOF;
    echo "DONE"
rsync -ave "ssh -p 199" /root/backups/files/$THESITE/daily/ 
[email protected]:/root/user/weekly
else
 echo "No weekly backups today"
fi

I get en error:

./backup.sh: 120: ./backup.sh: Syntax error: end of file unexpected (expecting "fi")

root@developementbox:~/backups#

It doesn't like fi and I don't understand what is wrong with this.

Upvotes: 0

Views: 875

Answers (1)

Richard Hughes
Richard Hughes

Reputation: 36

The EOF terminating your sftp commands must start at the beginning of the line and must not be terminated by ;

What is happening is that sftp continues to consume the rest of your shell script including the fi before returning control leaving your if condition unterminated as the fi has been incorrectly treated as an sftp command.

Remove spaces or tabs before EOF and the ; after all of the sftp commands including the EOF terminator and you should be good to go.

Upvotes: 2

Related Questions