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
When run, this program does the following: Please enter your name: Brian Hello, Brian! Notice these three important lines in the preceding program:
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
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 ArgumentsYou 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
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 SubstitutionAmong 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. |