wc prints a byte count for a nonexistent process

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

1 Answer

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like