Unix Shell Scripts   «Prev  Next»
Lesson 6 A command to read keyboard input
Objective Use the read command to collect user input.

Command to Read Keyboard Input

The read command is used to allow a user to interact with a shell script, typing a response to a question or other prompt that you include as part of your shell script. The information that the user enters is assigned to a variable so that it can be used within your script. Later in the course, you will learn in detail how to use variables, but here are two basic rules about variables that you create yourself:
  1. You can name a variable anything you want. Try to use descriptive names. All uppercase names are normally used to make them easy to see in your scripts. An example is INPUT or RESPONSE.
  2. When the name of a variable is used with a dollar sign in front of it, such as $INPUT or $RESPONSE, the value contained in the variable is referred to. This is true no matter how the variable was assigned.

The read command uses one parameter: the variable that the user’s entry should be stored in. For example, if you need to ask the user for the name of a file to work with, you could use these lines in your script:
echo Enter the name of the file to download:

The user sees the following text onscreen when the script runs:
Enter the name of the file to download: 

with the cursor waiting at the beginning of the next line for the user to enter a filename.
Once the user types something and presses Enter, the text is placed in the DOWNLOADFILE variable , which can be used in your shell script as needed. A simple example of using the variable value is to repeat back to the user what they entered, as verification (note the dollar sign, as described above):

echo The name of the file you entered was 

When this line is executed, the user sees something like this (depending on what was entered):
The name of the file you entered was 

Once the variable contains the text entered by the user, that information can be manipulated or tested in many ways, as you will learn about in future modules. The following section discusses using UNIX and the read command.

Using the read command

  1. Start a Bourne shell
  2. Enter the command to write the message “Please enter your name:”.
  3. Enter the command to read a line of text from the user into a variable called MYNAME.
  4. Enter your name on the blank line that appears.
  5. Enter the command to print the text “Thank you, “ followed by the value of the variable MYNAME.
  6. This completes the simulation.

First, as always, we need to go over the command syntax that we are going to use. The commands that we want to concentrate on in this chapter have to deal with while and for loops. When parsing a file in a loop, we need a method to read in the entire line of data to a variable. The most prevalent command is read. The read command is flexible in that you can extract individual strings as well as the entire line. Speaking of lines, the line command is another alternative to grab a full line of text. Some operating systems do not support the line command. For example, the line command is not available on OpenBSD and some versions of Linux and Solaris.
In addition to the read and line commands, we need to look at the different ways to use the while and for loops, which is the major cause of fast or slow execution times. A loop can be used as a standalone loop in a predefined configuration; it can be used in a command pipe or with file descriptors. Each method has its own set of rules. The use of the loop is critical to get the quickest execution times.

Read keyboard Input command - Exercise

Click on the Exercise link below to practice what you have learned in the UNIX Lab.
Read keyboard Input command - Exercise
The next lesson shows you how to prepare a script file for execution

SEMrush Software