bash EOF in if statement

2020-07-16 05:14发布

问题:

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?

回答1:

Drop the blanks and it should work:

    EOF
^^^^


回答2:

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

ftp -n $HOST <<EOF

to:

ftp -n $HOST <<-EOF


回答3:

EOL inside If-Else

Syntax of EOL can be this type

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

    EOL
fi