Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 869
Arthur Avatar asked Feb 19 '12 13:02

Arthur


People also ask

What is << EOF in bash?

This operator stands for the end of the file. This means that wherever a compiler or an interpreter encounters this operator, it will receive an indication that the file it was reading has ended. Similarly, in bash, the EOF operator is used to specify the end of the file.

How do you echo EOF?

Save this answer. Show activity on this post. There is no way to echo out an EOF. An EOF can only be generated either by reaching the end of a file or by invoking the keypress bound to the eof terminal setting ( Ctrl D by default) when the file being read is bound to the terminal.

What is EOF command?

the “end-of-file” (EOF) key combination can be used to quickly log out of any terminal. CTRL-D is also used in programs such as “at” to signal that you have finished typing your commands (the EOF command). CTRL-Z. key combination is used to stop a process. It can be used to put something in the background temporarily.


3 Answers

Drop the blanks and it should work:

    EOF
^^^^
like image 137
cnicutar Avatar answered Oct 06 '22 00:10

cnicutar


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

ftp -n $HOST <<EOF

to:

ftp -n $HOST <<-EOF
like image 40
Taleeb Avatar answered Oct 06 '22 00:10

Taleeb


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.

like image 34
kyakya Avatar answered Oct 06 '22 01:10

kyakya