Using EOF for Templating a bash script

by Hunter Lowe   Last Updated August 10, 2018 12:02 PM

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


Answers 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 !

Hunter Lowe
Hunter Lowe
August 10, 2018 11:54 AM

Related Questions


Running a php script in apache webserver

Updated February 23, 2017 21:02 PM



Restart Apache 2.4 using Bash Script on Ubuntu 15.10

Updated August 26, 2017 17:02 PM

:bad variable name read:

Updated July 02, 2018 22:02 PM