使用命令将字符串添加到 nginx.conf 中

use*_*600 4 scripting text-processing nginx

我需要include /etc/nginx/sites-enabled/*.confnginx.conf文件中添加这个字符串。它应该是这样的:

. . .
. . .

http {

    . . .
    . . .

    include /etc/nginx/conf.d/*.conf;
    # INCLUDE STRING HERE VIA COMMAND

}
Run Code Online (Sandbox Code Playgroud)

我想编写一些脚本,但我坚持这一步,不知道哪个命令可以为我做到这一点。

mau*_*wns 5

具体后,您可以附加模式sed

使用语法/pattern/ a <string>

sed '/include.*conf/ a "New string"'
Run Code Online (Sandbox Code Playgroud)

像这样:

echo "http {

    . . .
    . . .

    include /etc/nginx/conf.d/*.conf;

}" | sed '/include.*conf/ a "New string"'
http {

    . . .
    . . .

    include /etc/nginx/conf.d/*.conf;
"New string"

}
Run Code Online (Sandbox Code Playgroud)


如果要在特定位置追加,请使用(例如):

echo "http {

    . . .
    . . .


}" | sed '4 a\
    include /etc/nginx/sites-enabled/*.conf'
http {

    . . .
    . . .
    include /etc/nginx/sites-enabled/*.conf


}
Run Code Online (Sandbox Code Playgroud)

...附加在第四行之后。


sed 版本:

sed (GNU sed) 4.4
Run Code Online (Sandbox Code Playgroud)