Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

question on grep 4

Status
Not open for further replies.

sbrews

Technical User
Jun 11, 2003
413
US
when issuing a command like this:

ps -ef | grep inetd

I know that output will sometimes contain 2 lines: the match for inetd AND the grep command itself. Other times it will be just the results of the grep without the grep command.

IE Usually, you get this:

root 262290 90200 0 Oct 27 - 9:10 /usr/sbin/inetd
root 1310906 1609924 0 09:37:10 pts/4 0:00 grep inetd


but sometimes you will get this:

root 262290 90200 0 Oct 27 - 9:10 /usr/sbin/inetd


I dont quite understand why.

If anyone could explain or point me towards something that could, it would be appreciated.
 
As far as I'm concerned it's nothing more than a timing issue as to whether the ps picks up the grep. In any case you are advised to do something like
Code:
ps -ef | grep -v grep | grep inetd

Ceci n'est pas une signature
Columb Healy
 
or use a wildcard-list for a character of the search string

ps -ef|grep netd


HTH,

p5wizard
 
Wow, neat trick p5wizard. I've always hated that double call to grep!

Ceci n'est pas une signature
Columb Healy
 
columb - yes, I agree - its a timing issue.

I do know to do the grep -v or the netd trick that p5wizard suggests so that grep doesnt find itself, however...

I was recently asked to explain why the double output sometimes occurs and "its a timing issue" and "grep -v (or netd" to avoid the double output were not acceptable answers.

Since I seem to be knowledge deficient in that area, I am looking to learn why it sometimes displays as described above.
 
sbrews:

It may not be "acceptable" to you, but it's true. Executing a piped command such as:

ps -ef|grep inetd

spawns two commands. Both of them show up in the process table:

ps -ef
grep inetd

The timing issue is that the grep command actually sees itself in the process table although it's completed.

I've seen the phenomena with AIX, SCO, and Solaris, although the more modern *NIX seem to have elminated the problem. If you are not going to accept columb or p5wizard's suggestions, then live with it.

However, p5wizard that IS an excellent suggestion and worthy of a star.
 
olded - I didnt mean to imply that columb and p5wizards answers were unacceptable. On the contrary - those are the commands I would go with and recommend to others if asked. The people asking me the question didnt find that acceptable.

Your answer - in a nutshell - told me what I was looking for.

thanks
 
I read my reply, and, yes, I'm being way too harsh. I apologize; it comes from talking to machines all the time. Thanks for the star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top