Score:0

checking httpd syntax from within a shell script

kz flag

couldn't find this anywhere including on this boards. seems strange that nobody has run into the same need ever before. this should have a very simple answer.

trying to put the httpd -t output into a shell script variable. have tried all possible options that i know. stuff like:

r=`httpd -t` or r=$(httpd -t)

even tried:

for i in `httpd -t`

any other "normal" shell command like date or ls, etc works but i can't get that simple output to be put in a variable - doesn't matter if the output is an error, warning or just "Syntax OK".

essentially i just want to have a shell script that makes sure any programmatical edits to the httpd configuration have not screwed up the conf before restarting the server. i refuse to believe that this is not possible with a shell script...

thanks.

Score:1
pl flag

If I recall correctly (I don't have a machine with Apache installed in front of me right now), the output text from that command is going to stderr, not stdout. You need to redirect the output in order to capture it, like this:

r=$(https -t 2>&1)

The above will collect both stdout and stderr output. If you want only stderr without stdout, you need to do something like this:

r=$(https -t 2>&1 > /dev/null)

However, you don't actually need the text output in order to do what you want. If all you need to know is whether there is an error or not, you should just check the return code. If the configuration is error free it will return 0, if there is an error it will return some other number. From the man page for httpd:

Run syntax tests for configuration files only. The program immediately exits after these syntax parsing tests with either a return code of 0 (Syntax OK) or return code not equal to 0 (Syntax Error).

kz flag
@moche thanks for the reply. the httpd -t doesn't return any error codes. just stuff like 'Syntax OK' or 'AH00526: Syntax error on line 60....' but none of those can get checked since it can't be stored. i'll try the redirect. thanks.
pl flag
@pottyear I just added a quote from the manual that talks about the return codes. If you are not sure how to use return codes, see https://stackoverflow.com/a/26675771/829970 for an example.
kz flag
thanks for that
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.