A project I'm doing requires that I write a BASH shell script that checks what modules are installed in Apache (to make sure some requirements are met before an install is attempted), however, I had a HECK OF A TIME figuring out how to script the checking of the httpd -M command (the command that shows the installed Apache modules). I was tring to run the following on a CentOS 5 server:
# httpd -M | grep -c perl
And the results of the command were not being piped to grep. Instead, the results were being sent to the screen. Irritating! There's clearly a pipe character there. I kept thinking I was writing the command wrong or something. I tried the command on my Ubuntu desktop, and lo and behold it worked just fine:
$ sudo apache2ctl -M | grep -c perl
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
The "1" was the result I was looking for, because I knew mod_perl was installed.
Long story short, for some reason the CentOS 5 version of Apache sends the "httpd -M" results to stderr, rather then stdout. I have no freaking idea why it does this, because that's not error data. It seems like a bug in the HTTPD executable. The end result was that I needed to write my command as follows:
# httpd -M 2>&1 | grep -c perl
The 2>&1 sends the stderr data to stdout which is what the pipe character sends to the grep command.
How annoying. For the record:
# httpd -v
Server version: Apache/2.2.3
Server built: Oct 20 2011 17:00:12
Hope this helps someone avoid the frustration and confusion I went through finding it.