0

I came across a program called wc, which prints the number of bytes, words, and lines in files. Now my intention is to use it to determine if a process is actually running on my computer. If it is, it should print the number of bytes in the running process. Otherwise it should print 0. But I can use a fictitious process and it still prints bytes:

$ ps -ef | grep dfdsfdf | wc -c
74

Where is that 74 coming from?

1
  • What do you mean when you say print the number of bytes in the running process? The memory it is consuming? The size of the executable on disk?
    – jobin
    Mar 7, 2014 at 13:31

1 Answer 1

0

Ok

first of all

ps -ef 

will list every process on the system

Then you pipe the result and search for string "dfdsfdf"

The output will be nothing unless the command grep

hadi  28052 27027  0 08:54 pts/0    00:00:00 grep --color=auto dfdsfdf

Now you are passing this output to the pipeline and then counting the charters of this output and thus you obtain 74.

To check something remove one character from the string "dfdsfdf" then count become 73.

see:

ps -ef | grep dfdsfd | wc -c
73

Thanks to @steeldriver for his comment.

It might be worth adding that grep can be 'tricked' into not matching its own output by replacing the literal search string with a regex

ps -ef | grep [d]fdsfdf | wc -c

This will return 0

2
  • It might be worth adding that grep can be 'tricked' into not matching its own output by replacing the literal search string with a regex e.g. ps -ef | grep [d]fdsfdf | wc -c Mar 7, 2014 at 12:16
  • @steeldriver I just clarify what is going on here to the OP and not trying to solve but thanks I'll add your comment to the answer :)
    – Maythux
    Mar 7, 2014 at 12:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .