2

I have this script

 for ((i=0 ; $i<=$c ; i++))
 do
 cat <<"EOF" >my_file.html
 line 1
 line 2
 ...
 EOF
 done

It is supposed to write some specific lines in each loop but my file is empty and i always get this error

syntax error: unexpected end of file

Before this loop i have another use of cat exactly like this

cat <<"EOF" >my_file.html
line1
...
line n
EOF

After running the script my_file.html contains these n lines but none of the loop lines.

2 Answers 2

2

Your syntax is wrong.

You have a space before the delimiter denoting the end of here-doc.

 cat <<"EOF" >my_file.html
 line 1
 line 2
 ...
EOF     # Remove the leading space from this line.

Additionally, if you want to redirect within a loop you probably wanted to append >> instead of redirecting >.

Sign up to request clarification or add additional context in comments.

3 Comments

@user3301045 Yes, that's because you have a space before EOF (see edit above.
@user3301045 Show your script. There should be errors elsewhere.
@user3301045 As mentioned in the answer, you use > which truncates the file. In order to append to the file, use >>.
0

Make sure that you do not have any whitespace before or after the EOF delimiter, otherwise the shell won't be able to find the end of the here-document and you will get an unexpected end of file error.

This line should only contain the delimiter word (EOF) and nothing else. Not even comments.

cat <<"EOF" >my_file.html
line 1
line 2
EOF
# there should not be any whitespace around EOF on the line above

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.