Input and output
Input and output are often thought of in simple default terms:
keyboard/mouse and monitor/speakers. In UNIX, access to a system of input
and output streams (and an error stream) gives users and developers a way
to efficiently move input and output to and from applications, which
can streamline complex processes by minimizing human interaction.
stdin,
stdout, and
stderr are significant parts of what make UNIX
an ideal platform for scripting.
Most command-line applications can take input from
stdin and direct output to
stdout.
stdin stands for Standard Input.
stdout stands for Standard Output. By
default, input from stdin comes from the keyboard (what
you type in your terminal), and output to
stdout goes to your display (what
you see on your screen). An additional type of output,
stderr, prints errors and
debugs messages, but this tutorial focuses on
stdin and
stdout.
Redirection allows users to send output that would normally go to
stdout to another destination -- a file,
for instance. Create a text file of your TUTORIAL directory listing, as follows:
$ cd ~/TUTORIAL $ ls > listing.txt $ ls |
Another form of redirection is
>>, which appends a file as
opposed to creating a new file. You can also redirect
stderr to a file with
2> or to redirect all output (
stdin and
stderr) to a file with
&>. You can combine forms of
redirection; for instance, use
2>> to append
stderr to a file.
Now that you have a file with some text in it, look at what's inside.
The quickest way to examine the contents of a file is by using the
cat command, which stands for
Concatenate; it can be used in combination with redirection to
concatenate text. Type the following command:
$ cat listing.txt |
If you've executed each step in the tutorial, you should see something like this:
example.txt example2.txt listing.txt script.sh |
The previous cat command was executed on a
relatively small file, so you can easily see all the data on a single
page. If you're viewing a file that doesn't fit on a single page, the
more command is commonly used;
it pauses the output each time a full page of data is displayed to
stdout. Pressing the
Spacebar advances the output to the
next page. Try creating a longer file to use for an example:
$ ls /etc/ > listing2.txt $ cat listing2.txt $ more listing2.txt |
If you use cat, the file scrolls much too quickly to
read; but if you use more, you can press
the Spacebar to advance through the output step by step.
If you quickly want to view the first or last few lines of a file, you can use
head or tail. These commands are
commonly used to view the top of script or the bottom of a log file. It's also handy
to use them as a quick sanity check on an output file when you're debugging code. Try typing the following:
$ head listing2.txt |
To view the last few lines of a file instead of the first few lines, try testing
tail:
$ tail listing2.txt |
Both commands commonly default to displaying 10 lines, but you can use the
-n option to display any
number of lines. For instance, type this command:
$ head -n 2 listing2.txt |
Now that you're starting to create files with more data in them, you might
want to search for specific text.
grep is a powerful search utility
used here in a bare-bones example:
$ grep host listing2.txt |
This command outputs all the lines in listing2.txt that contain the
string host.
To skip the file-creation step, you can use the
pipe character
(|) to use the output of one command as the
input for another command. This is another form of redirection and is
incredibly powerful for linking long lists of commands to
efficiently manage input and output. Try this simple example:
$ ls /etc/ | grep host |
This command gives the same output as the two-step process listed previously. The
single line takes the output of the ls command and uses it as the search input
for the grep command. Here's another example:
$ du -sh /etc/* | more |
In this case, you check the disk usage (du) for each file and directory in
/etc/. The output is more than one page, so it's useful to
pipe the result to
more, which makes the output pause
each time one page of data is displayed.


