Grepping 'nginx -t' 结果

Chr*_*ris 2 nginx bash

奇怪的是,这对我不起作用。

nginx -t | grep -c 'successful' | awk '{if ($0==0) print "not found"; else if ($0>0) print "found"}'
Run Code Online (Sandbox Code Playgroud)

即使结果如下,它也会返回一个not found

nginx: [warn] conflicting server name "xxx.co.za" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "xxx.co.za" on 0.0.0.0:443, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
not found
Run Code Online (Sandbox Code Playgroud)

即使是一个简单的 grep 也不会做预期的事情..

(nginx -t | grep -q 'successful') && echo "Yes" || echo "Nope"
Run Code Online (Sandbox Code Playgroud)

知道如何检查nginx -t命令是否成功吗?

Joh*_*ald 6

通过 nginx 命令的退出值,如果成功则为 0。

root@nginx-1-vm:~# nginx -t && printf "Valid\n" || printf "Error\n"
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Valid
root@nginx-1-vm:~# mv /etc/nginx/nginx.conf /tmp
root@nginx-1-vm:~# nginx -t && printf "Valid\n" || printf "Error\n"
nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
Error
Run Code Online (Sandbox Code Playgroud)

比 grep 输出更好,因为消息可以在未来本地化或以其他方式更改。