Tuesday, May 10, 2005

Silencing grep

$ ps aux|grep xinit
kalyan 28918 0.0 0.0 1484 472 pts/2 R+ 17:37 0:00 grep xinit
$ ps aux|grep -q xinit

the -q option to grep is useful when you dont want to see the output in a script but want to just test for a match using the return value.

if $( ps aux|grep xinit); then echo "yo"; fi

Now this will try to run the output of that command $( ps aux|grep xinit)
and bash will say

-bash: kalyan: command not found

but now when you write a for loop when you have to use it else bash will complain.
So the right thing to do is

$ if ps aux|grep -q xinit; then echo "yo"; fi

$for mycom in $(ps aux|grep xinit); do echo "yo"; done


I know the examples are weird ;)

0 Comments:

Post a Comment

<< Home