如何使用sudo、EOF和静默输出将bash cat或tee输入到文件中

7

我基本上想编写一个设置虚拟主机的脚本,但是我无法弄清楚输出代码块到受保护根文件的语法,然后使输出静音。我一直在尝试类似于以下内容:

sudo tee /etc/apache2/other/$NAME.conf <<EOF
NameVirtualHost *:80
<Virtualhost *:80>
        ServerName $NAME.dev
        ServerAlias www.$NAME.dev
        DocumentRoot /var/www/$NAME

        <Directory "/var/www/$NAME">
                AllowOverride All
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>
EOF &> /dev/null

这条命令不会抑制输出,并且它会将 &> /dev/null 写入文件中。

1个回答

10
一个 Here document(这里文档)与 do-loop(循环)不同:重定向应该出现在带有命令的行上。
sudo tee /etc/apache2/other/$NAME.conf &>/dev/null <<EOF
NameVirtualHost *:80
<Virtualhost *:80>
        ServerName $NAME.dev
        ServerAlias www.$NAME.dev
        DocumentRoot /var/www/$NAME

        <Directory "/var/www/$NAME">
                AllowOverride All
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>
EOF

1
刚刚发现并来到这里回答了我的问题,这证实了它的正确性。谢谢! - AgmLauncher

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接