Next, you might ask what to do if you need to get input from the user. The Bourne shell syntax provides basic input with very little effort.
#!/bin/sh |
printf "What is your name? -> " |
read NAME |
echo "Hello, $NAME. Nice to meet you." |
You will notice two things about this script. The first is that it introduces the printf command. This command is used because, unlike echo, the printf command does not automatically add a newline to the end of the line of output. This is useful when you need to use multiple lines of code to output a single line of text. It also just happens to be handy for prompts.
Note: In most operating systems, you can tell echo to suppress the newline. However, the syntax for doing so varies. Thus, printf is recommended for printing prompts. See “Designing Scripts for Cross-Platform Deployment” for more information and other alternatives.
The second thing you'll notice is the read command. This command takes a line of input and separates it into a series of arguments. Each of these arguments is assigned to the variables in the read statement in the order of appearance. Any additional input fields are appended to the last entry.
You can modify the behavior of the read command by modifying the shell variable IFS (short for internal field separators). The default behavior is to split inputs everywhere there is a space, tab, or newline. By changing this variable, you can make the shell split the input fields by tabs, newlines, semicolons, or even the letter 'q'. This is demonstrated in the following example:
#!/bin/sh |
printf "Type three numbers separated by 'q'. -> " |
IFS="q" |
read NUMBER1 NUMBER2 NUMBER3 |
echo "You said: $NUMBER1, $NUMBER2, $NUMBER3" |
If, for example, you run this script and enter 1q3q57q65, the script would reply with You said: 1, 3, 57q65. The third value contains 57q65 because only three values are requested in the read statement.
Note: The read statement always stops reading at the first newline encountered. Thus, if you set IFS to a newline, you cannot read multiple entries with a single read statement.
Warning: Changing IFS may cause unexpected consequences for variable expansion. For more information, see “Variable Expansion and Field Separators.”
But what if you don’t know how many parameters the user will specify? Obviously, a single read statement cannot split the input up into an arbitrary number of variables, and the Bourne shell does not contain true arrays. Fortunately, the eval command can be used to simulate an array using multiple shell variables. This is described in “Data Structures, Arrays, and Indirection.”
Alternatively, you can use the for statement, which splits a single variable into multiple pieces based on the internal field separators. This statement is described in “The for Statement.”
C Shell Note: In the C shell, the syntax for reading is completely different. The following script is the C shell equivalent of the script earlier in this section:
printf "What is your name? -> " |
set NAME = $< |
echo "Hello, $NAME. Nice to meet you." |
sed as described in “Regular Expressions Unfettered.”Last updated: 2008-04-08