这个 cmd 行(Centos 7)有什么问题:
if (grep ssl_certificate /etc/nginx/conf.d/platform.conf)
then echo $(grep ssl_certc /etc/nginx/conf.d/file.conf) > /etc/nginx/conf.d/file-certs.conf
fi
Run Code Online (Sandbox Code Playgroud)
在 file.conf 中有 2 行带有 ssl_certificate 和 ssl_certificate_key。它应该将现有的 SSL 证书从 /etc/nginx/conf.d/file.conf 移动到 /etc/nginx/conf.d/file-certs.conf。
Kus*_*nda 19
代码片段在语法上没有任何错误,但很不寻常。
要使用 测试是否在文件中找到字符串grep,如果是这种情况,请执行某些操作,请使用
if grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf; then
Run Code Online (Sandbox Code Playgroud)
选项-q,-w并-F会使grep安静的(-q),并给定的模式匹配为一个固定的字符串,而不是一个正则表达式(-F)。此外,-w将 makegrep寻找一个完整的单词。在这种情况下,字符串ssl_certificate3将与模式不匹配。
grep如果在文件中找到字符串,将返回零退出状态,并且if语句的主体将被执行。
该声明
echo $(somecommand)
Run Code Online (Sandbox Code Playgroud)
有点没用。
命令替换$(somecommand)将由的输出来代替somecommand与外壳将执行字分割和文件名生成对所得串(这很可能不想要的)。使用这个 withecho是没用的,你本来可以做的
somecommand
Run Code Online (Sandbox Code Playgroud)
完整的if声明:
if grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf; then
grep -wF ssl_certc /etc/nginx/conf.d/file.conf >/etc/nginx/conf.d/file-certs.conf
fi
Run Code Online (Sandbox Code Playgroud)
(这假定这ssl_certc是一个完整的词。否则-w从那个词中删除grep)
或者,使用短路语法,
grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf &&
grep -wF ssl_certc /etc/nginx/conf.d/file.conf >/etc/nginx/conf.d/file-certs.conf
Run Code Online (Sandbox Code Playgroud)
请注意,如果该文件存在,这将覆盖其内容/etc/nginx/conf.d/file-certs.conf。更改>到>>要追加到文件,而不是,如果覆盖不是预期的。
| 归档时间: |
|
| 查看次数: |
2436 次 |
| 最近记录: |