在 nginx.conf
我有:
server {
listen 81;
...snip...
}
我希望文件看起来像这样:
server {
listen 81;
if (-f $document_root/system/maintenance.html) {
return 503;
}
...snip...
}
因此只有当文本 (if..
) 不存在时才应插入。
是否可以在命令行中使用一些内置程序?
类似于:
- 打开一个文件
- 看看里面有没有特定的文字
- 如果没有找到特定文本 (
listen 81;
) 并在它之后的下一行插入几行文本
最佳答案
这是 sed
的工作,包裹在一个 shell 脚本中。完全未经测试;调用为“./script/path/to/nginx.conf”;请注意,它不会寻找您想要添加的完整文本,只是从第一行开始寻找足够长的标记。
#! /bin/sh
if grep -q "document_root/system/maintenance" "$1"
then :
else
set -e
sed < "$1" > "$1.tmp" \
'/listen 81;/a\
if (-f $document_root/system/maintenance.html) {\
return 503;\
}
'
mv -f "$1.tmp" "$1"
fi
关于linux - 如何将文本插入到conf文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5546863/