Как я могу проверить, если файлы конфигурации Nginx действительны в скрипте Bash?

Поскольку у Дарвина отсутствует lesskey (см. здесь и здесь ), я установил less 458 через MacPorts и добавил привязки, используя значения по умолчанию путь для моей системы. lesskey примет $ HOME / .lesskey и выведет $ HOME / .less . Вы также можете использовать LESSKEY или LESSKEY_SYSTEM .

~ $ vi .lesskey
#command
\n        forw-screen
\40       forw-line
~ $ lesskey
~ $ ls -d .less*
.less       .lesshst    .lesskey

1
17.07.2018, 20:12
1 ответ

Использовать статус выхода. Из справочной страницы nginx:

Exit status is 0 on success, or 1 if the command fails.

и изhttp://www.tldp.org/LDP/abs/html/exit-status.html:

$? reads the exit status of the last command executed.

Пример:

[root@d ~]# /usr/local/nginx/sbin/nginx -t;echo $?
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
0
[root@d ~]# echo whatever > /usr/local/nginx/nonsense.conf
[root@d ~]# /usr/local/nginx/sbin/nginx -t -c nonsense.conf;echo $?
nginx: [emerg] unexpected end of file, expecting ";" or "}" in /usr/local/nginx/nonsense.conf:2
nginx: configuration file /usr/local/nginx/nonsense.conf test failed
1

Пример сценария:

#!/bin/bash
/usr/local/nginx/sbin/nginx -t 2>/dev/null > /dev/null
if [[ $? == 0 ]]; then
 echo "success"
 # do things on success
else
 echo "fail"
 # do whatever on fail
fi
5
27.01.2020, 23:15

Теги

Похожие вопросы