Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
Shells: User's Guide: HP 9000 Computers > Chapter 21 Basic Shell Programming

Data Input and Output

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Index

Programming inevitably requires inputting and outputting of data. The Korn Shell provides the echo command and the print command for outputting and the read command and positional parameter substitution for inputting.

Reading Input Data

There are several ways of passing data into a shell script. One way is by passing arguments to the script through positional parameters; the other way is by using the read command. A third way is for the script to run some command or program that reads stderr or a named file. Positional parameters have already been described in detail in Chapter 19 “Substitution Capabilities”. Therefore, the following discussion focuses mainly on the read command.

The read command provides the ability to read input during the execution of a script.

The read syntax for POSIX Shell is:

read [-r] name...

The read syntax for Korn Shell is:

read [-prsu[n]] [name?prompt] [name]...

where, in each case, the command reads a line and places each whitespace-separated word into a name. The rest of the line goes into the last name. For the Korn Shell, if names are not specified, the line is read into the shell REPLY variable (see “Using the select Statement”). If ?prompt is set, the user is prompted interactively with prompt.

Option definitions are:

-p

Read from the output of the process spawned with two-way pipes, |&. (See Chapter 23 “Advanced Concepts and Commands” for two-way pipes.)

-r

Do not interpret the \ at the end of a line as line continuation.

-s

Put the input line into the history file.

-un

Read the input from file descriptor n.

In this script contained in the file hello_script, the first line prints a prompt and waits for input:

read user_name?"What is your name? "
echo "Hello, $user_name, and welcome to Korn Shell."

The read command prompts the user for a name and stores the name in the variable user_name. Running the script creates this output:

$ hello_script
What is your name? Stefan
Hello, Stefan, and welcome to Korn Shell!

When you see the question mark, type in your name (Stefan is typed here to indicate user input), and then press Return.

The read command can read and store several values at one time:

read field1 field2 junk

This reads the first whitespace-separated name from the input line into field1, the second into field2, and the rest into junk.

Printing Data

Sometimes you may wish to output data or comments from a script on the screen, such as script results, and headers to describe the results. There are two output mechanisms in the shell. The first is the echo command used in Bourne, C, and POSIX Shells; and the second is the print command, unique to Korn Shell.

Using echo

The echo command prints its (expanded) arguments to stdout. The arguments are separated by spaces.

echo [arg ]...

The echo command will do parameter expansions on unquoted arguments, and on arguments in double quotes. It will not do expansion on arguments in single quotes.

$ var="This is var"
$ echo $var
This is var
$ echo "The value of var is: $var"
The value of var is: This is var
$ echo 'var is $var'
var is $var

You can also prompt a user from a script using the echo command and the \c linefeed escape character. The escape character suppresses the linefeed and leaves the cursor after the colon (:) and blank, waiting for input. Using this idea, type:

$ {
> echo "Enter your user name: \c"
> read user
> echo 'User is ' $user
> }
Enter your user name: Stefan
User is Stefan

Certain characters can be used for formatting echoed strings. These escape sequences are listed in Table 21-1 “echo Formatting Escape Sequences”.

Table 21-1 echo Formatting Escape Sequences

Escape Character

Results

\b

backspace

\c

print line without appending a newline

\f

form feed

\n

newline

\r

carriage return

\t

tab

\v

vertical tab

\\

backslash

 

Using print

Korn Shell provides a unique output mechanism the other shells do not: the print command. Its syntax is:

print [-Rnprsu[n]] [arg]...

The print command provides a superset of the echo command for shell output. It prints the specified args, depending on the option set. The options are:

-R

Ignore all echo escape sequences except \n.

-n

Do not add a newline to output.

-p

Write output to the process spawned with a two-way pipe, |&, instead of standard output (See Chapter 23 “Advanced Concepts and Commands” for two-way pipes.)

-r

Ignore all echo escape sequences.

-s

Save args in the history file.

-un

Write to the file descriptor n.

This print command:

$ print -s "# End of the day. $(date)"
$ history

puts the comment "# End of the day.", followed by a date, in your history file. This makes it easier to review the current day's command lines in the history file, because the end of yesterday's commands is clearly marked. (The history command lists the last sixteen command lines executed.)

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-1991 Hewlett-Packard Development Company, L.P.