Arthur
Arthur

Reputation: 931

bash EOF in if statement

I am trying to do an action in a IF ELSE statement in Bash, but receive an error like this one:

Syntax error: end of file unexpected (expecting "fi")

Now I am quite new to this, so probably the solution to my problem should not be that difficult :)

if [ "$DAYNAME" = 'Sunday' ]; then
    echo 'The backup will be uploaded'
    ftp -n $HOST <<EOF
        quote USER $USER
        quote PASS $PASSWD
        put $filetoday
        delete ${filethreeweeksago}
        quit
    EOF
fi

Of course the vars are already filled.

I think it has to do with the EOF notation, because when i remove them, the problem disapears. Unfortunatly I don't know how to use the code without the EOF notation.

Can anyone tell me why this error is comming up?

Upvotes: 11

Views: 21275

Answers (4)

Jess
Jess

Reputation: 3725

https://github.com/koalaman/shellcheck/wiki/SC1039

The here document delimiter will not be recognized if it is indented.

You can fix it in one of two ways:

1.Simply remove the indentation, even though this may break formatting.

2.Use <<- instead of <<, and indent the script with tabs only (spaces will not be recognized).

Removing the indentation is preferred, since the script won't suddenly break if it's reformatted, copy-pasted, or saved with a different editor.

Upvotes: 3

Ritesh Jhaggar
Ritesh Jhaggar

Reputation: 113

EOL inside If-Else

Syntax of EOL can be this type

if [ condition ]; then
    echo "Comment"
else
    cat >> file.txt <<-EOL

    EOL
fi

Upvotes: 1

Taleeb
Taleeb

Reputation: 135

add a dash to EOF if you want to keep the tabs: from:

ftp -n $HOST <<EOF

to:

ftp -n $HOST <<-EOF

Upvotes: 7

cnicutar
cnicutar

Reputation: 182664

Drop the blanks and it should work:

    EOF
^^^^

Upvotes: 34

Related Questions