Difference between revisions of "Stdout, stderr and stdin"
imported>ThorstenStaerk m (Stdin, stdout and stderr moved to Stdout, stdin and stderr) |
imported>ThorstenStaerk m (Stdout, stdin and stderr moved to Stdout, stderr and stdin) |
(No difference)
|
Revision as of 11:04, 12 January 2010
Piping is a famous concept in Unix that describes the handling of input and output of running processes. Think of a process that asks for your name. You enter your name by using the keyboard. But instead of reading from the keyboard, the process could also read from the standard input stream. And by default, the standard input stream could be fed by the keyboard. You can then exchange the keyboard against the output of another process.
I mean the following. Here you start a process that asks you for your name and greets you then:
tweedleburg:~ # (echo -n "how is your name? "; read name; echo "hallo $name") how is your name? Thorsten hallo Thorsten tweedleburg:~ #
You type in the name on the keyboard and the command reads from standard input which comes from the keyboard.
Now comes the trick. You can also use the output of another process to be the standard input (stdin) for this process. Instead of writing to the console, the process writes to the input of the next process. You do this with a pipe:
tweedleburg:~ # echo "process number $$" | (echo -n "how is your name? "; read name; echo "hallo $name") how is your name? hallo process number 16939 tweedleburg:~ #
Here, the echo process writes "process number 16939". Normally, it would write to console, but the pipe ("|") redirects the output to the input of the next process (enclosed in parentheses). This process would normally read from keyboard, but there is already another stream coming from standard input. So it reads "process number 16939" just as if you had entered it on the keyboard.