![]() |
| |||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
A Unix command receives its input from an input stream opened by the shell. Similarly, it writes its output to an output stream, again opened by the shell. The important point is that the command does not open input and output streams itself - it accepts them from the shell. The terms standard in (or stdin) and standard out (or stdout) are used to refer these streams. Normally standard in is the keyboard and standard out is the Terminal window. Naturally, a command might open a file to read or write in preference to standard in or out, but many of the utility commands do work from the standard streams. This allows the shell, and ultimately yourself, to control from where a command reads its input, and to where it writes its output. Try this. % catType a few lines of input. When 'return' is pressed the line just typed is echoed back. (Press control-d once when you get bored.) What is happening? In this example an input file is not specified. 'cat' reads standard in (the keyboard) and writes each line to standard out (the Terminal window). Copy cat perhaps? The command: % cat letter.txtwrites the contents of 'letter.txt' to the Terminal window. This time 'cat' was given a filename to read in preference to standard in. It continues to write to standard out. The shell allows one to redirect standard in and standard out from/to a file. The command: % cat letter.txt > copy-of-letter.txteffectively copies letter.txt to copy-of-letter.txt. The '>' character redirects standard out to the specified file. It is important to realise that as far as 'cat' is concerned: % cat letter.txtand % cat letter.txt > copy-of-letter.txt are the same. The shell implements redirection. The shell opens/creates file 'copy-of-letter.txt' and passes this as standard out to 'cat'. Cat unknowingly writes to the file instead of the Terminal window. Redirection works for all commands that write to the Terminal window. For example: % ls -al > ls.txtwrites a directory listing to file 'ls.txt'. Double redirection occurs when one uses '>>' to redirect, instead of '>'. The only difference being the specified file is added to instead of being overwritten. For: % cat letter.txt > copy-of-letter.txtthe shell creates a new file called 'copy-of-letter.txt', and removes any existing file of that name. For: % cat letter.txt >> copy-of-letter.txtthe shell opens the existing file 'copy-of-letter.txt', and standard out is written to the end of the file leaving the original contents intact. If the file does not exist it is created. Just like standard out, standard in can be redirected so that a file is read instead of the keyboard. Redirecting standard in is not done as often as redirecting standard out. It is used mainly for commands that take no filenames and expect to use stdin and stdout. Such a command is 'tr'. The real value of having standard in is lies in 'piping' described later. The symbol to redirect standard in is '<'. 'tr' translates one character to another. Try this % tr 'p' 'a'then type 'apple' followed by control-d. A typical use for 'tr' is to translate files that have Mac-style end of line characters (carriage-return) into files that have Unix-style end of line characters (new-line). In this example, input is read from 'mac-file' and output is written to 'unix-file'. We must therefore redirect both standard in and standard out, as 'tr' takes no filenames. The special character '\r' is used by 'tr' to represent carriage-return. The command is this. % tr '\r' '\n' < mac-file > unix-fileTake 'mac-file', translate each carriage-return to line-feed, and write the result to 'unix-file'. Note: because 'tr' processes a line at a time, it is not possible to write back to the file being read. If you wish to do this use the following trick: tr '\r' '\n' < file > tmp; mv tmp fileThe ';' character separates two commands on a single line, and is often used when forming an alias that executes many commands. |
Tell Me More...
|
|
cat Remember from a previous tutorial that the 'cat' command is designed to concatenate files. It takes a list of files as input and joins them together by sending them one after another to the standard output. Question: What is the difference between: % cat letter.txtand % cat < letter.txtAnswer: To the user, none. To the shell and 'cat' this... -> cat letter.txt 'cat' opens the file letter.txt and reads from it. letter.txt is not made the standard input. Standard input is still the keyboard but 'cat' ignores it. -> cat < letter.txt 'cat' sees no input file and therefore reads standard in. It is the shell that interprets: '< letter.txt'opening file 'letter.txt' and passing it as standard in (instead of the keyboard) to 'cat'. Standard Error Unix commands write error and warning messages as well as regular output. Often it is desirable to separate the two, especially in shell scripts because regular output is processed, but error messages are written to the Terminal window. To enable this a command actually has two output streams available, standard out and standard error (or stderr). Normally both of these are set by the shell to be the Terminal window. Redirect Standard Out and Error Redirecting standard out does not redirect standard error too. Try this: % cat zxzxzYou will get an error message (assuming 'zxzxzx' does not exist). % cat zxzxz > outwill create an empty 'out' file and the error message still appears on the terminal. To redirect standard out and standard error use: % cat zxzxz >& outTo redirect them to different files use the following trick: % ( cat letter.txt > out ) >& err(All one one line). This works by creating a subcommand that redirects standard out to 'out', then the output of the subcommand (now only standard error) is in turn redirected to 'err'. |
Next Page
Next page I will introduce 'pipelines'. Piping allows one to join commands together by directing the output of the first command to the input of the following command.
|
|
Part 7 - Redirection and Pipelines (page 1 of 2) |
|