Why the following if statement succeeds ?
if $(ps aux | grep -q "bla bla") ; then echo "found" ; fi
Why the following if statement succeeds ?
if $(ps aux | grep -q "bla bla") ; then echo "found" ; fi
Because the grep process itself is being returned by ps. You can "trick" grep to not match itself by surrounding one of the search characters in a character class [ ] which doesn't change the functionality:
Just do:
if ps aux | grep -q "[b]la bla" ; then echo "found" ; fi
Also, the use of process substitution $() is unnecessary. The if will work on the success of the last command in the pipe chain, which is what you want.
Note: The reason the character class trick works is because the ps output still has the character class brackets but when grep is processing the search string, it uses the brackets as syntax rather than a fixed string to match.
If you grep the output from ps aux, you will always get a process showing your previous command. To fix this, you can pipe the output to grep twice, once to remove line with "grep" in it, and again for the process your looking for.
ps aux | grep -v "grep" | grep "Finder"
the 'grep' process is already running by the time ps runs, so the ps output includes it.
Try using pgrep instead.
pgrep is precisely for this purpose:
if pgrep "bla bla" ; then echo "found" ; fi
The $( is a small little bit relevant, and changes the meaning a bit. Although in this case, because there is never any output from grep -q, you can just about get away with the $(. You probably want to start with something like (as pointed out by others):
if ps aux | grep -v 'grep' | grep -q 'bla bla'; then
echo 'Found'
fi
Anyway, you started with
if $(ps aux | grep -q "bla bla") ; then echo "found" ; fi
With $(, the command inside the $( ) is executed and the output of that command is used as the command line for the outer command. Do these four experiments:
# if $(echo nonexistant ; true) ; then echo "found" ; fi
nonexistant: command not found
# if $(echo nonexistant ; false) ; then echo "found" ; fi
nonexistant: command not found
# if $(echo ; true) ; then echo "found" ; fi
found
# if $(echo ; false) ; then echo "found" ; fi
So, according to this you will output get found if both these conditions hold:
$( ) created no outputThis suggests that ps aux | grep -q "bla bla" was successful and created no output. It's no surprise that grep -q creates no output. That's what the -q is for. So therefore, your command must have had a true status, which implies that the grep did successfully find a match. We know that grep will always find a match in this case, because the list of processes from ps will include grep itself; the grep will always find itself.
You need to filter out the process that is grepping for 'bla bla':
$ if ps aux | grep -v 'grep' | grep -q 'bla bla'; then
echo 'Found'
fi