0

I am attempting to create a simple script which will generate files in /etc/apache2/sites-available directory using a for loop, and get it to do this using a template within my script. This is what the snippet of my script looks like:

cat <<EOF > /etc/apache2/sites-available/${VHOST}.com.conf 
<VirtualHost *:80>
ServerName ${VHOST}.com
ServerAlias www.${VHOST}.com
ServerAdmin [email protected]
DocumentRoot /var/www/${VHOST}.com
ErrorLog ${APACHE_LOG_DIR}/${VHOST}_error.log
CustomLog ${APACHE_LOG_DIR}/${VHOST}_access.log combined
Options -Indexes
</VirtualHost>
EOF 

NOTE: ${VHOST} is simply a value specified within my for loop.

The result which I am trying to achieve is that the template output will be sent to a file ${VHOST}.com.conf within the sites-available directory.

My above snippet does not work and I receive the following errors:

./vhost.sh: line 21: warning: here-document at line 6 delimited by end-of-file (wanted `EOF')
./vhost.sh: line 21: syntax error near unexpected token `newline'
./vhost.sh: line 21: `<<-EOF <VirtualHost *:80>'

UPDATE:

I followed @PerlDuck's suggestion by updating the file as follows

cat > /etc/apache2/sites-available/${VHOST}.com.conf <<-EOF
<VirtualHost *:80>
ServerName ${VHOST}.com
ServerAlias www.${VHOST}.com
ServerAdmin [email protected]
DocumentRoot /var/www/${VHOST}.com
ErrorLog ${APACHE_LOG_DIR}/${VHOST}_error.log
CustomLog ${APACHE_LOG_DIR}/${VHOST}_access.log combined
Options -Indexes
</VirtualHost>

Attempting cat > /etc/apache2/sites-available/${VHOST}.com.conf <<- "EOF" also did not work. Both produce the same error.

./vhost.sh: line 22: warning: here-document at line 6 delimited by end-of-file (wanted `EOF')
./vhost.sh: line 23: syntax error: unexpected end of file
4
  • See askubuntu.com/a/806750/504066. You need cat > ${VHOST}.com.conf <<EOF...EOF
    – PerlDuck
    Aug 10, 2018 at 11:42
  • This does not produce the desired outcome. Will update my question Aug 10, 2018 at 11:46
  • Sorry, I was wrong about the "must not" comment I wrote a minute ago. But you have an extra space after the last END . That's the error.
    – PerlDuck
    Aug 10, 2018 at 11:50
  • Heredocs (that's their name) are peculiar. ;-)
    – PerlDuck
    Aug 10, 2018 at 11:54

1 Answer 1

1

@PerlDuck resolved my issue for me. This is simply a case of unwanted blank space. This extra whitespace after the final EOF prevents the heredoc from terminating. Thus producing the error

./vhost.sh: line 22: warning: here-document at line 6 delimited by end-of-file (wanted `EOF')
./vhost.sh: line 23: syntax error: unexpected end of file

Remember kids! Blankspace, blankspace, blankspace !

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .