an Unix shell script with only purpose – count the number of running processes of qmail (could be anything else). Easy thing, but there must be some bug in code:
#!/bin/bash
rows=`ps aux | grep qmail | wc -l`
echo $rows
Because
echo $rows
always shows greater number of rows (11) than if I just count rows in
ps aux | grep qmail
There are just 8 rows. Does it work this way on your system too?
No, since I’m not running
qmail. However, you will want to, at a bare minimum, exclude the process running your grep:For debugging, you may want to do:
(to see your input to the script in great detail) and then rewrite your script temporarily as:
That way, you can see the input and figure out what effect it’s having on your code, even as you debug it.
A good debugger will eventually learn to only change one variable at a time. If your changing your code to get it working, that’s the variable – don’t let the input to your code change as well.