我想知道是否有更好的方式来创build一个文件,而不是每行附加到文件。 我这样做,所以我可以保持可读性,但是,没有缩进。 有没有办法一次创build一个文件并input多行?
if [ -d "/srv/www/$1" ]; then echo "Domain name already exists!" else mkdir -p /srv/www/$1/public_html; mkdir -p /srv/www/$1/logs; echo "<VirtualHost>" > /etc/apache2/sites-available/$1 echo "ServerAdmin support@$1" >> /etc/apache2/sites-available/$1 echo "ServerName $1" >> /etc/apache2/sites-available/$1 echo "ServerAlias www.$1" >> /etc/apache2/sites-available/$1 echo "DocumentRoot /srv/www/$1/public_html/" >> /etc/apache2/sites-available/$1 echo "ErrorLog /srv/www/$1/logs/error.log" >> /etc/apache2/sites-available/$1 echo "CustomLog /srv/www/$1/logs/access.log combined" >> /etc/apache2/sites-available/$1 echo "</VirtualHost>" >> /etc/apache2/sites-available/$1 a2ensite $1
使用heredoc。
cat > /etc/apache2/sites-available/"$1" << EOF <VirtualHost> ServerAdmin support@$1 ... EOF
正如答案#1中所述的Heredoc,或者只是让你的回声跨越多行
echo "line 1 line2 line3" > file
如果它是可读性,那么你有没有考虑把它分成多个文件? 有一个“模板”文件,您可以编辑它与您的贝壳复制。
## /path/to/vhtemplate <VirtualHost> ServerAdmin support@#1 ServerName #1 ServerAlias www.#1 DocumentRoot /srv/www/#1/public_html/ ErrorLog /srv/www/#1/logs/error.log CustomLog /srv/www/#1/logs/access.log combined </VirtualHost>
示例脚本:
if [ -d "/srv/www/$1" ]; then echo "Domain name already exists!" else mkdir -p /srv/www/$1/public_html; mkdir -p /srv/www/$1/logs; cp /path/to/vhtemplate /etc/apache2/sites-available/$1 sed -i -e 's/#1/$1/' /etc/apache2/sites-available/$1 a2ensite $1