View and vote on the article here: Shell Programming Series(IV)
Shell Programming Series(IV)| Category | | | Summary | | Simply assigning variables inside a script and calling those variables later is not very useful. 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 redirec |
| | Body | Interacting with the User
The command that can read input from a user is read. Here is a slightly modified version of the ?Hello World? program that reads from STDIN, which is normally the keyboard:
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
11. exit 0
When run, this program does the following:
Please enter your name: Ismail
Hello, Ismail!
There are three new aspects of this program that should be considered:
? 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, so therefore the string is stored in the variable exactly as the user enters it with all whitespace preserved.
? Line 9: The echo command is used to send the string Hello, followed by the contents of the variable name. Once again, the curly braces are optional.
The read command can take multiple variables as arguments. When it does, whitespace will be used as the delimiter to separate what goes into each variable. Following is an example:
#!/bin/sh
echo
echo ?n ?Enter three numbers separated by spaces or tabs: ?
read var1 var2 var3
echo
echo ?The value of variable1 is: ${var1}?
echo ?The value of variable2 is: ${var2}?
echo ?The value of variable3 is: ${var3}?
echo
exit 0
Here is a sample run of the previous program:
Enter three numbers separated by spaces or tabs: 117 1024 23098
The value of variable1 is: 117
The value of variable2 is: 1024
The value of variable3 is: 23098
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 argument separators 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
The value of variable1 is: 1
The value of variable2 is: 2
The value of variable3 is: 3 4 5 6 7
In the next chapter we will deal with command-line arguments.
|
|
This article was imported from zZine. (original author: ismail)
There are no replies to this post yet.
|