Skip to content

            Lost ?  |  Need an account:
 
Home >> Knowledge Base >> Operating Systems >> Linux >> Stream Redirection - Standard Input
Stream Redirection - Standard Input PDF Print E-mail
(2 votes, average 5.00 out of 5)
Written by Tom Hirt   
Tuesday, 24 February 2009 13:06
Article Index
Stream Redirection
Standard Input
Standard Output
Standard Error
Combined Output
All Pages

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.

$ touch some_file.txt
$ rm -i some_file.txt
rm: remove regular empty file `some_file.txt'? yes
$ echo $?
0

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.

$ cat command_list
yes

$ touch some_file.txt

$ ls
command_list  some_file.txt

$ rm -i some_file.txt 0< command_list
rm: remove regular empty file `some_file.txt'?

$ echo $?
0

$ ls
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:

$ cat command_list
yes

$ touch some_file.txt

$ ls
command_list  some_file.txt

$ rm -i some_file.txt < command_list
rm: remove regular empty file `some_file.txt'?

$ echo $?
0

$ ls
command_list

Here is another example where we display the contents of a file:

$ cat 0< some_file.txt
This is the contents of some_file.txt

$

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:

$ wall << MY_DELIMITER
> hi everyone,
> we have maintenance scheduled in the next 5 minutes.
> we will be rebooting the system.
>
> Please save all work immediately!
>
> Thanks,
> -sysadmin
> MY_DELIMITER
$
Broadcast message from thirt (Wed Feb 25 16:51:46 2009):

hi everyone,
we have maintenance scheduled in the next 5 minutes.
we will be rebooting the system.

Please save all work immediately!

Thanks,
-sysadmin

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.



Comments
Add New Search
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:D:):(:0:shock::confused:8):lol::x:P:oops::cry::evil::twisted::roll::wink::!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

!joomlacomment 4.0 Copyright (C) 2009 Compojoom.com . All rights reserved."

Last Updated on Monday, 02 March 2009 15:57
 

Forum Activity

Author: Cogterrit
May.18.12
Author: Cogterrit
May.18.12
Author: Cogterrit
May.18.12
Author: Cogterrit
May.17.12

Online Stats

Guests Online: 80
Members Online: 1