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:
- 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
- When the name of a variable is used with a dollar sign in front of it, such as
$RESPONSE, the value contained in the variable is referred to. This is true no matter how the variable was assigned.
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
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.
- Start a Bourne shell
- Enter the command to write the message “Please enter your name:”.
- Enter the command to read a line of text from the user into a variable called MYNAME.
- Enter your name on the blank line that appears.
- Enter the command to print the text “Thank you, “ followed by the value of the variable MYNAME.
- 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.