When I automate actions using Bash, I often use the return value of an Unix command to test things like : If a string is present in a file (grep) or if a process is running (ps aux + grep).
Here is an exemple, grep will return 1 if nothing matches and 0 if there is at least one result.
$ ps aux | grep process_that_doesntexist | grep -v grep
$ echo $?
1
$ ps aux | grep init | grep -v grep
root 1 0.0 0.1 135188 6660 ? Ss 01:43 0:00
$ echo $?
0
Is it a bad habit/way of programming ? Should bash scripts written this way should run on production servers ?
Thanks.
$?.ps, by the way, see also ProcessManagement, particularly section 3.2 if you're writing software responsible for making sure a service is running -- there are tools specifically built for the purpose built into almost all operating systems.