nginx如何用shell获取测试结果
習慣沉默
習慣沉默 2017-05-16 17:15:34
[Linux讨论组]

想写一个自动脚本
先测试生成的配置文件

/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/test.conf

如果测试成功就覆盖nginx.conf 不成功就返回错误

然而不知道如何获取执行测试指令之后的信息 指令都是执行成功

習慣沉默
習慣沉默

全部回复(1)
黄舟
[root@arbiter nginx]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@arbiter nginx]# echo $?
0

[root@arbiter nginx]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/mime.types
nginx: [emerg] "types" directive is not allowed here in /usr/local/nginx/conf/mime.types:2
nginx: configuration file /usr/local/nginx/conf/mime.types test failed
[root@arbiter nginx]# echo $?
1

bash 中 $? 用来获取上一条命令的返回值,0 为正常,其他值为异常。

如果需要获取命令输出,可以按一下方式操作:

[root@arbiter nginx]# a="`/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/mime.types 2>&1`"
[root@arbiter nginx]# echo $a
nginx: [emerg] "types" directive is not allowed here in /usr/local/nginx/conf/mime.types:2 nginx: configuration file /usr/local/nginx/conf/mime.types test failed

其中2>&1的作用是将标准错误重定向到标准输出

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号