我正在尝试创建一个简单的脚本,它将/etc/apache2/sites-available
使用 for 循环在目录中生成文件,并使用脚本中的模板来执行此操作。这是我的脚本片段:
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
注意:${VHOST} 只是我的 for 循环中指定的一个值。
我想要实现的结果是模板输出将被发送到${VHOST}.com.conf
sites-available 目录内的文件中。
我的上述代码片段不起作用,并收到以下错误:
./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>'
更新:
我按照@PerlDuck 的建议更新了文件如下
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>
尝试cat > /etc/apache2/sites-available/${VHOST}.com.conf <<- "EOF"
也无济于事。两者都产生相同的错误。
./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
答案1
@PerlDuck 帮我解决了这个问题。这只是一个不需要的空白的情况。最后一个 EOF 之后的这个额外空白会阻止 heredoc 终止。因此产生了错误
./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
记住孩子们!空白,空白,空白!