DistributedNetworks DistributedNetworks

Unix Shell Scripts   «Prev 

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.

read Command

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.