I want to check the validity of my nginx configuration using nginx -t
using it as a condition for a bash script.
if [[ $(nginx -t) = *"successful"* ]]; then
echo "success";
else
echo "failure";
fi
While the output of nginx -t
contains successful
, bash prints failure
regardless. What am I doing wrong?
EDIT: I don’t want to check by exit code but by string output.
Solution:
if [[ $(nginx -t 2>&1) = *"successful"* ]]; then
echo "success";
else
echo "failure";
fi
```
2
Answers
Here is my (working) script to do the same:
You are expecting the message to printed on stdout. It is printed on stderr. Redirect stderr to stdout to capture the message.
If you want to capture stderr and stdout of a command and also check it’s exit status, do: