| Stream Redirection - Standard Input |
|
|
|
| Written by Tom Hirt | ||||||||||||
| Tuesday, 24 February 2009 13:06 | ||||||||||||
Page 2 of 5
Standard Input or STDIN STDIN is typically taken from your keyboard. However, using input redirection, we can redirect the contents of a file as the input for a command. This is quite simply accomplished by appending a "0< FILENAME " to the end of a give command: For example, we see in the following diagram a typical STDIN scenario where a prompt seeking STDIN, asks the user to provide input via the keyboard in order to remove the file some_file.txt.
Using this example, we can redirect STDIN providing an answer file so that the command completes automatically without prompting for confirmation via the keyboard. In the following example, we have created a text file named command_list that contains one line with the word "yes". We again run the command rm but this time redirecting STDIN to our answer file, command_list.
The results are exactly as we expected. The command completed successfully, removing the file some_file.txt while receiving it's input to the confirmation prompt from our answer file, command_list. We can further simply the syntax by omitting the "0" with the same result:
Here is another example where we display the contents of a file:
We can also make use of a delimiter by the use of a '0<< DELIMITER' (the zero can be omitted to simplify the syntax.) For example:
In the above example, we used the delimiter MY_DELIMITER to provide a multi-line input stream to wall to announce scheduled maintenance to our users. As you can see, the input stream begins with the first delimiter MY_DELIMITER and terminates with the second MY_DELIMITER, everything in-between is sent as STDIN to wall. |
||||||||||||
| Last Updated on Monday, 02 March 2009 15:57 |