我正在创建一个脚本,该脚本读取文件的内容,操作内容并将其追加到另一个文件(特别是虚拟主机文件)。输出文件中的格式和空格很重要,但是当我编写内容时,空格会被删除。
VHOST_PATH="/etc/apache2/extra/httpd-vhosts.conf"
TEMPLATE_PATH="./template.conf"
TEMPLATE=$(<TEMPLATE_PATH)
# manipulating $TEMPLATE
echo $TEMPLATE #outputs correct whitespace
echo $TEMPLATE >> $VHOST_PATH #does not output correct whitespace
所以第一次回声会产生类似
<VirtualHost *:80>
ServerAdmin webmaster@domain
DocumentRoot "root/web"
ServerName domain
ErrorLog "root/logs/error_log"
</VirtualHost>
但是进入文件的字符串是
<VirtualHost *:80> ServerAdmin webmaster@domain DocumentRoot "root/web" ServerName domain ErrorLog "root/logs/error_log" </VirtualHost>
在追加到目标文件时如何保留空白?我已经进行了搜索,但是所有类似的问题都不会在不重写脚本的情况下应用于我的脚本。
echo $TEMPLATE
行之有效。它也应该折叠多个空格。