Piping and Redirection

前端 未结 5 2134
挽巷
挽巷 2020-12-01 03:15

What is exact difference between piping and redirection?

Where should we use piping and where should we use redirection?

How they internall

5条回答
  •  感情败类
    2020-12-01 03:35

    Basically redirection and piping are a few ways among many to achieve Inter Process communication in Unix.

    1. Redirection: Data is written to and read from a typical UNIX file. Any number of processes can interoperate. this must be used when sharing large data sets.

    ls > FileName

    1. Piping : Piping is a process where the output of one process is made the input of another. They were evolved in the most primitive forms of the Unix operating system. They provide unidirectional flow of communication between processes within the same system. A pipe is created by invoking the pipe system call, which creates a pair of file descriptors. [ For file descriptors read http://www.bottomupcs.com/file_descriptors.html ]

    ls | grep $myName

    It works on simple data sharing, such as producer and consumer.

    Property Comparison: Piping is always uni-directional while redirection could be used to redirecting input as well as output.

    ls > grep myFileName [ Redirecting output of first command to later one ] sort < fileName.txt [ Redirecting fileName.txt file as an input to command sort ]

    One can also write below to use bi-directional redirect in single statement.

    sort < fileName.txt > sortNewFile.txt

    While Piping, it is always output of first command supplied to the later one and that to simulanoeously.

    ls | grep myName | awk '{ print $NF }' [ multiple piping in a single statement ]

    Note 1: command > fileName . If there is a command named fileName, that would make using redirection a lot harder and more error prone. One must check first, whether there's a command named like destination file.

    Other ways to achieve IPC in Unix system are:

    1. Named pipe
    2. Signal
    3. Shared memory
    4. Socket

提交回复
热议问题