Interacting with the User


You now know how to assign variables to contain different data; but without input from the user, your variables won't ever change. The shell provides a way for you to get input from STDIN. Normally, this input will be typed in by the user (or received from a file if STDIN has been redirected). The command that can read input from a user is read. Listing 10.3 shows a slightly modified version of the "Hello World!" program that reads some input from STDIN, which is normally the keyboard, and then acts on that input.

Listing 10.3. Reading Input from STDIN

1.  #!/bin/sh 2. 3.  # Modified Hello World program that accepts input from keyboard 4. 5.  echo 6.  echo -n "Please enter your name: " 7.  read name 8.  echo 9.  echo "Hello, ${name}!" 10. echo

When run, this program does the following:

Please enter your name: Brian Hello, Brian!


Notice these three important lines in the preceding program:

  • Line 6 A new option to the echo command is introduced. The -n option suppresses the newline character that would normally be sent at the end of the echo statement. This causes the cursor to remain on the same line after Please enter your name:.

  • Line 7 The read command accepts input from STDIN (in this case, the keyboard). The user can enter a string of text here. When Enter is pressed, read takes whatever the user entered and stores it in the variable name. The read command implies quotes; therefore the entered string is stored in the variable exactly as the user enters it with all whitespace preserved.

  • Line 9 The echo command is used to insert the contents of the variable name into the string beginning with Hello, and ending with the exclamation point; then, once the final string is composed, it sends the string to the screen. Once again, the curly braces are optional.

The read command can take multiple variables as arguments. When it does, whitespace is the delimiter that separates what goes into each variable. Listing 10.4 shows an example.

Listing 10.4. Using Multiple Variables with read

1.  #!/bin/sh 2. 3.  echo 4.  echo -n "Enter three numbers separated by spaces or tabs: " 5.  read var1 var2 var3 6.  echo 7.  echo "The value of var1 is: ${var1}" 8.  echo "The value of var2 is: ${var2}" 9.  echo "The value of var3 is: ${var3}" 10. echo

Here is a sample run of the preceding program:

Enter three numbers separated by spaces or tabs: 557 2024 57240 The value of var1 is: 557 The value of var2 is: 2024 The value of var3 is: 57240


It doesn't matter how much whitespace you use in the input from the keyboard. When assigning multiple variables, read interprets any amount of whitespace as an argument separator to determine what should go in each variable.

If read gets fewer arguments than there are variables in its list, the remaining variables after the argument list runs out will not be assigned. On the other hand, if read gets more arguments than it has variables, whatever arguments are left over will be assigned to the last variable in the list. For example, here is another run of the program that gives read more than three arguments:

Enter three numbers separated by spaces or tabs: 1 2 3 4 5 6 7 8 9 0 The value of var1 is: 1 The value of var2 is: 2 The value of var3 is: 3 4 5 6 7 8 9 0


Handling Command-Line Arguments

You can also get information from the user by reading arguments included on the command line. The shell automatically stores up to nine command-line arguments in special internal variables. No programming is required to actually read the argumentsyou just need to address the variables $1 through $9. The FreeBSD POSIX shell also allows additional arguments that can be accessed with ${10}, ${11}, and so on. But this should be avoided if the program will need to run on other systems because the traditional Bourne shell can only handle $1 tHRough $9. The variable $0 contains the name of the program itself, the variable $@ contains a list of all the arguments, and the variable $# contains the number of arguments that were passed to the program. Listing 10.5 shows an example.

Listing 10.5. Command-Line Arguments in a Script

1.  #!/bin/sh 2. 3.  echo 4.  echo "The name of the program is: $0" 5.  echo "The total number of arguments received is: $#" 6.  echo "The complete argument string is: #@" 7.  echo "Your first name is: $1" 8.  echo "Your last name is: $2" 9.  echo

And here is a sample run:

# ./yourname Brian Tiemann The name of the program is: ./yourname The total number of arguments received is: 2 The complete argument string is: Brian Tiemann Your first name is: Brian Your last name is: Tiemann


Note

The POSIX shell that FreeBSD uses also supports the getopts command, which provides a more versatile way of handing command-line arguments with a shell program. We will discuss this command when we talk about the advanced features of Korn shell programming. If you are writing Bourne shell programs (that use sh) for other UNIX platforms, however, it is best to avoid the use of getopts because it is not a standard part of the traditional Bourne shell and may cause the program not to work on some systems.


Command Substitution

Among other things, command substitution allows you to run a command and assign the output to a variable. The command to be run should be enclosed in backward quotes (`). Be careful not to confuse this character with the single quote. The backward quote character typically shares a key with the tilde (~) character on the keyboard. Here's an example of the backward quote used to enclose a command:

todaysDate=`date`


This example runs the date command and assigns its output to the variable todaysDate. You can then access the information in this variable just as you would with any other variable.




FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net