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
74Where is that 74 coming from?
11 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 dfdsfdfNow 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
73Thanks 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 -cThis will return 0
2