[sf-lug] ps and grep

jim stockford jim at well.com
Thu Mar 6 18:00:32 PST 2008


yes. for any command that uses piping, the bash shell
loads the right-most command first, then works its way
back to the left-most. you can verify this by looking at
the process numbers:
$ ps aux | grep " " | grep "  "

the shell loads grep with an argument to look for any
line with two space characters, then it loads grep with
an argument to look for any line with one space
character, then it loads the ps command with its aux
argument.



On Mar 6, 2008, at 5:48 PM, Tom Haddon wrote:

>
> On Thu, 2008-03-06 at 17:47 -0800, jim stockford wrote:
>>
>> at the point you type the  ps  command and hit
>> enter, you've kicked off a new process, something
>> like
>> $ ps aux | grep doit
>>
>> the pipe character is also a command delimiter:
>> you've kicked off two processes, the first one loaded
>> is the  grep  process and the second one loaded is
>> the  ps  process. after all, nothing runs unless it's a
>> process, and for  ps  to get the output of  grep  then
>> grep has to be loaded first so's to be in place, right?
>
> Do you mean that for grep to process ps's output, grep has to be loaded
> first? If so, this all makes sense, and I think I have it.
>
> Thanks! Tom
>
>>
>> but it's only after those two processes are loaded
>> that the  ps  command starts its work, which means
>> it's dumping all processes including the  grep
>> process and the  ps  process itself. you've asked
>> grep to filter out everything but any line that has
>> doit on it, so if the doit process is running, you get
>> that line, and you also get the line for which doit is
>> an argument: the grep process.
>>
>> make sense?
>>
>>
>>
>> On Mar 6, 2008, at 5:17 PM, Tom Haddon wrote:
>>
>>> Bit of an elementary question, this, but can someone remind me why:
>>>
>>> ps fuwxx | grep <something>
>>>
>>> returns "grep <something>" in the list if finds? Intuition would
>>> suggest
>>> that the ps is happening first, and so the grep command wouldn't show
>>> in
>>> the list. One of those things that was explained to me once, but 
>>> seems
>>> to have slipped through my sieve-like memory...
>>>
>>> Cheers, Tom
>>>
>>>
>>>
>>> _______________________________________________
>>> sf-lug mailing list
>>> sf-lug at linuxmafia.com
>>> http://linuxmafia.com/mailman/listinfo/sf-lug
>>>
>>
>
>





More information about the sf-lug mailing list