Sunday, 17 June 2012

Explain I/O redirection, Piping and Command Substitution


Piping

Commands can be connected together using pipes, which causes the output of one command to be fed into the input of the next. Both commands run concurrently.
| means connect stdout to stdin of the next command. Errors still come to the shell window.
|& means connect both stdout and stderr to stdin of the next command.



I/O redirection

By default, when csh runs a command, the command inherits the csh's stdio file handles for stdin, stdout and stderr, which normally all point to the console window where the C shell is running. The i/o redirection operators allow the command to use a file instead for input or output.
> file means stdout will be written to file, overwriting it if it exists, and creating it if it doesn't. Errors still come to the shell window.
>& file means both stdout and stderr will be written to file, overwriting it if it exists, and creating it if it doesn't.
>> file means stdout will be appended at the end of file.
>>& file means both stdout and stderr will be appended at the end of file.
< file means stdin will be read from file.
<< string is a here document. Stdin will read the following lines up to the one that matches string.



Command substitution

Command substitution allows the output of one command to be used as arguments to another.
`command` means take the output of command, parse it into words and paste them back into the command line.

No comments:

Post a Comment