Nwlapcug.com


Tutorial Unix pipe



Il tubo di comando UNIX funziona come un tubo di fisico. Si connette due comandi UNIX e così facilita un flusso unidirezionale di dati. Come un tubo di fisico, che controlla il flusso del fluido, il comando pipe offre agli utenti controllo dell'output dei comandi in esecuzione.

Nozioni di base

Tutorial Unix pipe


Proprio come un fisico giunto di due condutture che dà l'illusione di un tubo continuo, il comando di pipe (|) consente due comandi UNIX lavorare insieme. Per esempio:

$ cat abc.txt | wc --l
23

The UNIX command "cat" is used to retrieve the contents of a file, while the "wc -l" command does a word count of the input stream of words. The pipe command "|" in this example passes the output of the "cat" of file abc.txt (which lists down the contents of the file abc.txt) to "wc -- l," which performs a word count on the contents. In the example, the result is 23, which means the file abc.txt contains 23 words. If these commands were executed separately, it would be difficult to count the number of words in the file abc.txt. Pipe lets the user achieve this by combining the two commands.

In un giunto di fisica della pipeline, l'output di un tubo non può essere lo stesso come input per l'altro tubo, come illustrato nel diagramma. Allo stesso modo in UNIX, è possibile che la quantità di output di un comando potrebbe non corrispondere la quantità di input di altro. Il comando pipe vi dà la possibilità di controllare il flusso. Se un comando agisce più velocemente rispetto agli altri, riaggiusta il flusso. Per esempio:

$ ls | more

The "ls" command lists the contents of a directory. However, this may result in excess output on the terminal if the directory consists of lots of files. Therefore, in order to control the output and make more sense of it, the pipe command (|) is used to pass this output to another command, "more" (which limits the output to a page). This results in the output being shown in one page. Only when the user presses "Enter" is the next page of output shown.

Flessibilità

Il comando pipe ha la capacità di unire più di due comandi di UNIX, fornendo maggiore flessibilità all'utente. Tuttavia, per utilizzare questo, gli utenti devono sapere quello che vogliono nel complesso e che cosa di singoli comandi. Per esempio:

$ls | grep ^d | grep notes | wc --l

The command above intends to find all sub-directories inside a directory that contain the word "notes" in them and then does a word count. This is done by the combination of the four commands shown above. The first command, "ls," lists the content of the current directory. The second command, "grep ^d," finds the sub-directories from that list. The third command, "grep notes," finds the sub-directories that contain the word "notes." The fourth command, "wc --l," does a word count from the output.

Il punto principale da ricordare durante l'utilizzo il comando pipe è che l'output del comando a sinistra è sullo standard output e l'input per il comando a destra è dallo standard input. Quindi se ci sono i comandi UNIX che inviano uscite di non uscite standard (terminali, per esempio) in cui viene eseguito il comando, il comando pipe non deve essere utilizzato.

Programmazione

Condutture possono essere creati nei programmi C/C++, troppo, utilizzando il metodo di () di tubo. Questa chiamata crea un oggetto di tubo, che crea due nuovi descrittori di file e fd1, fd2. Una lettura da fd1 accede i dati scritti fd2 su base FIFO (First In First Out), e una lettura da fd2 accede i dati scritti fd2 su base FIFO.