想寫一個自動腳本
先測試產生的設定檔
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/test.conf
如果測試成功就覆蓋nginx.conf 不成功就回傳錯誤
然而不知道如何取得執行測試指令之後的資訊 指令都是執行成功
黄舟2017-05-16 17:17:34
[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
的作用是將標準誤差重新導向到標準輸出