Friday, July 25, 2014

Pipes in Linux/Unix

Command pipeline

  • The output of 1 command is directed to the input of the another to form a pipeline.
    A pipe is used by the shell to connect the stdout of one command directly to the stdin of another command.
    Example
  • STDERR is not passed down the pipe (BY Default)
  • The symbol for a pipe is the vertical bar ( | ). The command syntax is:
  •  
    
         command1 [arguments]  |  command2 [arguments]
    
    
  • Pipes accomplish with one command what otherwise would take intermediate files and multiple commands. For example, operation 1 and operation 2 are equivalent:
  •  
    
    Operation 1
    
         who > temp
    
         sort temp
    
    
    
    Operation 2
    
         who | sort
    
    
  • Pipes do not affect the contents of the input files.
  • Two very common uses of a pipe are with the "more" and "grep" utilities. Some examples:
     
    
         ls -al | more
    
         who | more
    
         ps ug | grep myuserid
    
         who | grep  kelly
    
    
  • Diverting stream through tee
    tee enables the stream to be diverted.
    Example:
    ls | tee somefile | sort | more

Building commands

  • Shells provide glue to build complex commands


References

0 comments:

Post a Comment