Bash脚本:如何将一个部分添加到配置文件中?

时间:2012-10-23 13:08:26

标签: linux bash

我知道如何查找和替换配置文件中的部分,如下所示:

sudo sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 3M/" /etc/php5/cgi/php.ini

但是如何在bash脚本中的配置文件末尾添加一个全新的部分?

server {
    listen      xx.xx.xx.xx:80;                # your server's public IP address
    server_name  myapp.com;                   # your domain name
    root         /usr/share/nginx/www/myapp/;  # absolute path to your WordPress installation    
}

我在终端echo "server { ... }" >> nginx.conf内尝试了这个,但我得到了

-bash: nginx.conf: Permission denied

不确定在bash脚本中是否可以,或者我会再次拒绝权限...

非常感谢,

2 个答案:

答案 0 :(得分:0)

您使用的是正确的语法。

echo "....." >> ngix.conf

确保您拥有&ng; ngix.conf'的正确写入权限。

  

-bash:nginx.conf:权限被拒绝

这表示您没有写权限。

您也可以将该部分写在一个文件中,例如' new_section.txt'并且晚点添加:

cat new_section.txt >> ngix.conf

但是我不建议以这种方式向ngix添加配置。 您可以使用' include'言。

include you_conf_path/my_new_config.conf;

通过这种方式,您可以对配置进行分组,并拥有一些小配置文件而不是一个大文件。然后,您还可以以帮助您找到正确文件的方式命名文件。

答案 1 :(得分:0)

试试这个:

config="server {\n    listen      xx.xx.xx.xx:80;\n     server_name  myapp.com;               \n    root         /usr/share/nginx/www/myapp/;  =installation    \n }\n"
 in1="text_before_required_input"
 out1=`echo -e $config`;
              in=$in1 out=$out1 perl -pi.nk -e 's/\Q$ENV{"in"}/$ENV{"out"}/g'  file

如果不是上述内容,请查看GNU ed http://wiki.bash-hackers.org/howto/edit-ed