Why do not all commands work with pipes?

3

I'm intrigued that kill and some other commands do not work with pipe. For example, I tried cat /tmp/server.pid | kill -9 and it did not work. Looking for some forums in English I found the solution: kill -9 $(cat /tmp/server.pid) . Beauty, problem solved.

However, the question remained. Like kill , there are other commands that do not work with pipe usage, such as rm and ls . I read somewhere that pipe only works with commands that expect input from stdin . But taking into account that stdin corresponds to the keyboard, all commands do not accept input from stdin? It sounds like a pretty basic thing ... but I'm confused about it.

    
asked by anonymous 23.03.2016 / 13:50

2 answers

1

Strictly speaking, no program is required to treat data coming from stdin (ie via keyboard or pipe ). A program can be built to treat data from a pre-defined file, read a port, or simply do nothing.

If you want, it can read stdin data. In the same way, if you want, it can read data from the command line that was used to start it, which is what kill does.

The reason most Unix / Linux commands treat stdin is because of Unix philosophy :

  

"Write programs that do one thing and do it well.   that work together. Write programs to treat text strings,   because this is a universal interface. "

It is a philosophy that encourages linking programs as a sequence of filters, to be able to give varied treatment to arbitrary sets of data.

But not all Unix / Linux commands have reason to follow. This has to do with the type of action that the command executes, and whether it has the filter behavior or not. This answer provides more details.

    
23.03.2016 / 16:20
3

Interpret this way, let's use the kill command as the first example:

Let's say the output of the command "cat /tmp/server.pid" is something like "3193". As you did the first time, your command with pipe + kill would look something like: "3193 kill -9"; however we know that this will not work, because we have to execute the command "kill -9" followed by a parameter, in the case of the pid of the process we want to terminate; so the correct thing would be to turn this parameter into a variable, which is not possible with the use of the pipe, as it is used to "concatenate" and / or redirect commands.

The way you use the pipe depends exclusively on how you organize your commands, see:

"As a Linux beginner, I realized the relevance of the pipe to make shell commands more practical and easier to write and porting. I decided to share my experience with this command.

The pipe is one of the ways Linux can use for inter-process communication. In a simplistic way we could say that the pipe is nothing more than the thread of processes. At first glance the pipe may not attract attention from beginners, but it is a very powerful tool. This process thread can be activated by the user through the "|" command. Now let's demonstrate in the example below the potentiality of this tool:

$ ls | grep b | sort -r | tee file.out | wc -l

  • The "ls" command, as we all know, lists the contents of the directory, but due to the pipe it does not send the result to the screen, command "grep b".
  • The "grep b" command in turn filters the names of files containing the letter "b". Due to the second pipe the command "grep b" is sent to "sort -r", which sorts the names in increasing order.
  • The output of "sort -r" is then passed to the command "tee", which divides the data into two, as if it were a connection in t, causing the information processed by the command "sort -r" are written in the file "file.out".
  • Then the command "wc -l" the lines of the file "file.out". Thus we obtain as result the number of files that contain the letter "b" printed on the screen and the name of these files in "file.out".

(...)

Reference: Using the pipe

    
23.03.2016 / 14:42