Bash: Check output of command

bash, linux

Solution

I know this is the old thread and the question doesn't suit this particular site. Anyway, looking for the same question, this page was shown as the first search result. So, I am posting here my final solution, for a reference.

configtestResult=$(sudo apachectl configtest 2>&1)

if [ "$configtestResult" != "Syntax OK" ]; then
    echo "apachectl configtest returned the error: $configtestResult";
    exit 1;
else
    sudo apachectl graceful
fi

This thread contains a clue regarding catching configtest output.

Problem

I need to check the output of `apachectl configtest` in a bash script and restart if everything looks good without outputting the command to the screen ``` var =sudo apachectl configtest ``` If var contains "Syntax OK" then ``` sudo apachectl graceful ``` How to do it?

Original source

Related problems