Bash将多行回显到文件末尾,但不在终端中回显

Bash将多行回显到文件末尾,但不在终端中回显,bash,macos,terminal,Bash,Macos,Terminal,我正在编写一个自定义别名函数,以便在创建新web项目时为我创建vHost。在我的函数中,我想将整个新虚拟主机粘贴到我的httpd vhosts.conf文件中。我首先要求输入域名和文件夹名,以便像这样创建 read -p 'Please enter the domain for your new site: ' domain; read -p 'Please enter the folder name for your new site: ' folder; echo " <Vir

我正在编写一个自定义别名函数,以便在创建新web项目时为我创建vHost。在我的函数中,我想将整个新虚拟主机粘贴到我的
httpd vhosts.conf
文件中。我首先要求输入域名和文件夹名,以便像这样创建

read -p 'Please enter the domain for your new site: ' domain;
read -p 'Please enter the folder name for your new site: ' folder;
echo "
   <VirtualHost *:80>
     ServerAdmin $domain
     DocumentRoot \"/Users/sam/Development/Websites/$folder\"
     ServerName $domain
     ErrorLog \"/usr/local/var/log/httpd/$folder/error_log\"
     CustomLog \"/usr/local/var/log/httpd/$folder/access_log\" common
   </VirtualHost>" | sudo tee -a /usr/local/etc/httpd/extra/httpd-vhosts.conf;
然后在我的函数中,我像这样写入我的
httpd vhosts.conf
文件

read -p 'Please enter the domain for your new site: ' domain;
read -p 'Please enter the folder name for your new site: ' folder;
echo "
   <VirtualHost *:80>
     ServerAdmin $domain
     DocumentRoot \"/Users/sam/Development/Websites/$folder\"
     ServerName $domain
     ErrorLog \"/usr/local/var/log/httpd/$folder/error_log\"
     CustomLog \"/usr/local/var/log/httpd/$folder/access_log\" common
   </VirtualHost>" | sudo tee -a /usr/local/etc/httpd/extra/httpd-vhosts.conf;
echo”
ServerAdmin$域
DocumentRoot\“/Users/sam/Development/Websites/$folder\”
ServerName$域
ErrorLog\“/usr/local/var/log/httpd/$folder/error\u log”
自定义日志\“/usr/local/var/log/httpd/$folder/access\u log\”通用
“| sudo tee-a/usr/local/etc/httpd/extra/httpd-vhosts.conf;
但是我注意到这个回音在终端和
conf
文件中。该功能工作正常,但如何使其不在终端中回音


是否必须使用除<代码> Engule<代码>之外的其他东西来添加到文件中以实现此?

< P>而不是<代码> TE/<代码>您可以考虑使用<代码> > <代码>

sudo sh -c "echo \"
   <VirtualHost *:80>
     ServerAdmin $domain
     DocumentRoot \"/Users/sam/Development/Websites/$folder\"
     ServerName $domain
     ErrorLog \"/usr/local/var/log/httpd/$folder/error_log\"
     CustomLog \"/usr/local/var/log/httpd/$folder/access_log\" common
   </VirtualHost>\" >> test2.out"
sudo sh-c“echo”
ServerAdmin$域
DocumentRoot\“/Users/sam/Development/Websites/$folder\”
ServerName$域
ErrorLog\“/usr/local/var/log/httpd/$folder/error\u log”
自定义日志\“/usr/local/var/log/httpd/$folder/access\u log\”通用
\“>>测试2.out”

只需重定向到
/dev/null

echo "
   <VirtualHost *:80>
     ServerAdmin $domain
     DocumentRoot \"/Users/sam/Development/Websites/$folder\"
     ServerName $domain
     ErrorLog \"/usr/local/var/log/httpd/$folder/error_log\"
     CustomLog \"/usr/local/var/log/httpd/$folder/access_log\" common
   </VirtualHost>" | sudo tee -a /usr/local/etc/httpd/extra/httpd-vhosts.conf > /dev/null
echo”
ServerAdmin$域
DocumentRoot\“/Users/sam/Development/Websites/$folder\”
ServerName$域
ErrorLog\“/usr/local/var/log/httpd/$folder/error\u log”
自定义日志\“/usr/local/var/log/httpd/$folder/access\u log\”通用
“| sudo tee-a/usr/local/etc/httpd/extra/httpd-vhosts.conf>/dev/null

这允许您附加
sudo
权限,同时也不会在屏幕上显示输出。

我只是用这种方法得到了“权限被拒绝”?我做了一个小改动,您能再试一次吗?这很有效,但是,在
/etc/hosts
上使用此方法时,是否有任何原因导致使用此方法仍会抛出
权限
错误?此处与
/etc/hosts
无关。这是不常见的,我无法重现它运行
sudo sh-c“…”
是一个主要的安全缺陷(使用非常量字符串,使用双引号替换紧跟在
-c
之后的参数)。如果您有,比如说
folder='$(rm-rf/*)'
,那么在字符串被替换为传递给
sh-c
的参数后,该扩展将作为root运行。它使用起来更安全,只为
tee
tee
升级权限,其目的是同时写入文件和标准输出(就像一个管道三通:水单向流入,双向流出)。使用
sudo-tee
升级用于打开文件的权限是一种副作用,而不是主要的使用/设计案例。