Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Hour 13
Input/Output

Until now you have been looking at commands that print out messages. When a command prints a message, the message is called output. In this chapter, you will look at the different types of output available to shell scripts. This chapter also introduces the mechanisms used to obtain input from users.

Specifically, the areas that you will cover are

  Output to the screen
  Output to a file
  Input from a file
  Input from users

Output

As you have seen in previous chapters, most commands produce output. For example, the command

$ date

produces the current date in the terminal window:

Thu Nov 12 16:32:35 PST 1998


When a command produces output that is written to the terminal, we say that the program has printed its output to the Standard Output, or STDOUT. When you run the date command, it prints the date to STDOUT.

You might have also seen commands produce error messages, such as:

$ ln -s ch01.doc ch01-01.doc
ln: cannot create ch01-1.doc: File Exists


Error messages are not written to STDOUT, but instead they are written to a special type of output called Standard Error or STDERR, which is reserved for error messages. Most commands use STDERR for error messages and STDOUT for informational messages.

You will look at STDERR later in this chapter. In this section you will look at how shell scripts can use STDOUT to output messages to each of the following:

  The terminal (STDOUT)
  A file
  The terminal and a file

Output to the Terminal

Two common commands print messages to the terminal (STDOUT):

  echo
  printf

The echo command is mostly used for printing strings that require simple formatting. The printf command is the shell version of the C language function printf. It provides a high degree of flexibility in formatting output.

You first look at echo and then printf.

echo

The most common command used to output messages to the terminal is the echo command. Its syntax is

echo string

Here string is the string you want printed. For example, the command

$ echo Hi

produces the following output:

Hi

You can also embed spaces in the output as follows:

$ echo Safeway has fresh fruit
Safeway has fresh fruit

In addition to spaces, you can embed each of the following in the string:

  Punctuation marks
  Variable substitutions
  Formatting escape sequences

Embedding Punctuation Marks   Punctuation marks are used when you need to ask the user a question, complete a sentence, or issue a warning. For example the following echo statement might be the prompt in an install script:

echo Do you want to install?

Usually, significant error messages are terminated with the exclamation point. For example, the echo command

echo ERROR: Could not find required libraries! Exiting.

might be found in a script that configures a program for execution.

You can also use any combination of the punctuation marks. For example, the following command uses the comma (,), question mark (?), and exclamation point(!) punctuation marks:

$ echo Eliza, where the devil are my slippers?!?
Eliza, where the devil are my slippers?!?

Embedding Variable Substitution   Chapters 7, “Variables,” and 8, “Substitution,” cover variable substitution, which is the process that the shell uses to substitute in the value of a variable where that variable’s name occurs. Often variable substitution is embedded in an echo command, where part or all of the text of the message that is displayed depends on the value of a variable.

A common use of this technique is in the display of pathnames:

echo Your home directory is $HOME
Your home directory is /home/ranga

In this example, you used the simplest form of variable substitution, the $ character, but you can use any form of variable substitution.


Formatting with Escape Sequences   In the previous examples, the output consisted of single lines with words separated by spaces. Frequently, output needs to be formatted into columns or multiple lines. By using escape sequences you can format the output of echo. An escape sequence is a special sequence of characters that represents another character. When the shell encounters an escape sequence, it substitutes the escape sequence with a different character. The echo command understands several formatting escape sequences, the most common of which are given in Table 13.1.
Table 13.1 Escape Sequences for the echo Command

Escape Sequence Description

\n Prints a newline character
\t Prints a tab character
\c Prints a string without a default trailing newline

The \n escape sequence is usually used when you need to generate more than one line of output using a single echo command. You usually use this when you need to print a list preceded by a description of the list. For example, the command

$ FRUIT_BASKET="apple orange pear"
$ echo "Your fruit basket contains:\n$FRUIT_BASKET"
Your fruit basket contains:
apple orange pear

generates a list of fruit preceded by a description of the list.


Previous Table of Contents Next