Previous Section  < Day Day Up >  Next Section

12.2. Reading User Input

The read command is used to take input from the terminal or from a file until the newline is reached. The Korn shell provides some additional options for the read command. See Table 12.1 for different read formats. See Table 12.2 for read options.

Example 12.5.

(The Script)

     #!/bin/ksh

     # Scriptname: nosy

     print –n "Are you happy? "

1    read answer

     print "$answer is the right response."

     print –n "What is your full name? "

2    read first middle last

     print "Hello $first"

     print –n "Where do you work? "

3    read

4    print I guess $REPLY keeps you busy!

5    read place?"Where do you live? "

     # New ksh read and print combined

     print Welcome to $place, $first $last





(The Output)

     $ nosy

     Are you happy? Yes

1    Yes is the right response.

2    What is your full name? Jon Jake Jones

     Hello Jon

3    Where do you work? Tandem

4    I guess Tandem keeps you busy!

5    Where do you live? Timbuktu

     Welcome to Timbuktu, Jon Jones


Table 12.1. read Formats

Format

Meaning

read answer

Reads a line from standard input and assigns it to the variable answer.

read first last

Reads a line from standard input to the first whitespace or newline, putting the first word typed into the variable first and the rest of the line into the variable last.

read response?"Do you feel okay?"

Displays the string Do you feel okay? to standard error and waits for the user to type a reply, then puts the reply in the variable response. This form of read requires and accepts only one variable. Whatever the user types, until the newline, will be stored in response.

read –u3 line

Reads a line from file descriptor 3 into variable line.

read

Reads input into a built-in variable, REPLY.


Table 12.2. read Options

Options

Meaning

–p

Reads a line of input from a coprocess.

–r

Treats newline character, the \n, as a literal.

–s

Copies a line into the history file.

–un

Reads from file descriptor n; the default is fd 0, or standard input.

On Versions of ksh Newer Than 1988

–A

Stores the fields as an array, index starting at zero.

–d char

Used as an alternate delimiter for terminating input; newline is the default.

–t sec

Puts a limit of seconds on the user's response time.


EXPLANATION

  1. The read command accepts a line of user input and assigns the input to the variable answer.

  2. The read command accepts input from the user and assigns the first word of input to the variable first, assigns the second word of input to the variable middle, and all the rest of the words to the end of the line to the variable last.

  3. The read command, without an argument, accepts a line of input from the user and assigns the input to the built-in variable REPLY.

  4. After the shell has performed variable substitution, the print function prints the string, showing the value of the built-in REPLY variable.

  5. If the variable following the read command is appended with a question mark (?), the string after the question mark is displayed as a prompt. The user input is stored in the variable place.

12.2.1 read and File Descriptors

When the system boots up, three files called streams (stdin, stdout, and stderr) are opened and assigned to an array of file descriptors. The first three file descriptors, 0, 1, and 2, are for standard input, standard output, and standard error, respectively. The next file descriptor available is file descriptor 3. The –u option allows the read command to read directly from the file descriptor.

Example 12.6.

(The Command Line)

1    $ cat filex

     Captain Kidd

     Scarlett O'Hara

2    $ exec 3< filex      # filex is assigned to file descriptor 3 for reading

3    $ read –u3 name1     # read from filex and store input in variable, name1

4    $ print $name1

     Captain Kidd

5    $ read –u3 name2

     $ print $name2

     Scarlett O'Hara

6    $ exec 3<&-         # close file descriptor 3

7    $ read -u3 line

     ksh: read: bad file unit number


EXPLANATION

  1. The contents of filex are displayed.

  2. The exec command is used to open file descriptor 3 for reading from filex.

  3. The read command reads one line directly from unit 3 (file descriptor 3, filex) and assigns that line to the variable name1.

  4. The line stored in name1 is printed.

  5. The file filex is still open, and this read command reads the next line from the file and stores that line in the variable name2.

  6. File descriptor 3 (unit 3) is closed. filex is no longer open.

  7. Because file descriptor 3 (filex) has been closed, the read command fails when attempting to read input from that descriptor into variable line.

12.2.2 Reading Through Files

Example 12.7 uses the read command with a while loop. The loop will iterate through the file one line at a time. When end of file is reached, the loop terminates. The files are opened with descriptors (units) for reading.

Example 12.7.

(The Files)

1    $ cat names

     Merry Melody

     Nancy Drew

     Rex Allen

     $ cat addresses

     150 Piano Place

     5 Mystery Lane

     130 Cowboy Terrace

------------------------------------------------------------------

(The Script)

     #!/bin/ksh

     # Scriptname: readit

2    while read -u3 line1 && read -u4 line2

     do

3        print "$line1:$line2"

4    done 3<$1 4<$2

------------------------------------------------------------------

(The Command Line)

5    $ readit names addresses

     Merry Melody:150 Piano Place

     Nancy Drew:5 Mystery Lane

     Rex Allen:130 Cowboy Terrace

---------------------------------------------------------------


EXPLANATION

  1. The contents of two files, names and addresses, are displayed.

  2. The while loop is started. The read command reads a line of input from file descriptor 3 (unit 3) and, if successful, reads another line from file descriptor 4. The file descriptors (units) are assigned filenames on line 4. The filenames are being passed as arguments, or positional parameters 1 and 2.

  3. The value of the first variable, a colon, and the value of the second variable are displayed.

  4. The input assigned to file descriptor 3 is the first command-line argument, names. The input assigned to file descriptor 4 is the second command-line argument, addresses.

  5. The script is executed with command-line arguments (the names of two files).

    Previous Section  < Day Day Up >  Next Section