11.2. Reading User Input

11.2.1. The $< Variable

To make a script interactive, a special TC shell variable is used to read standard input into a variable. The $< symbol reads a word from standard input up to the first white space but not including the newline, and assigns the word to a variable. By placing $< in double quotes (or parentheses)[1], a whole line is read, not including the newline.

[1] The C shell does not rerquire double quotes around the $< variable to read a whole line.

Example 11.4.
(The Script - greeting)
   #!/bin/tcsh -f
   # The greeting script
1  echo -n  "What is your name? "
2  set name = "$<"
3  echo Greetings to you, $name.

(The Command Line)
   > chmod +x greeting
   > greeting
1  What is your name?
						Dan Savage
3  Greetings to you, Dan Savage
					

Explanation

  1. The string is echoed to the screen. The -noption causes the echo command to suppress the newline at the end of the string. On some versions of echo, use a c at the end of the string to suppress the newline; e.g., echo "helloc".

  2. Whatever is typed at the terminal, up to the newline is stored as a string in the name variable.

  3. The string is printed after variable substitution is performed.

11.2.2. Creating a Wordlist from the Input String

Because the input from the $< variable is stored as a string, you may want to break the string into a wordlist. You can also use the Linux head command to read input from the user and store it as a wordlist. (See "Command Substitution" on page 580.)

Example 11.5.
1  > echo What is your full name?
2  > set name = "$<"
						Daniel Leo Stachelin
   
3  > echo Hi $name[1]
						Hi Daniel Leo Stachelin
   
4  > echo $name[2]
						Subscript out of range.
   
5  > set name = ( $name )

6  > echo Hi $name[1]
   Hi Daniel
   
7  > echo $name[2] $name[3]
   Leo Stachelin
   
8  > echo Where do you live?
9  > set city = `head -1`
						Chico, California
   
10 > echo "$city[1] is a college town, isn't it?"
					

Explanation

  1. The user is asked for input.

  2. The special variable $< accepts input from the user in a string format.

  3. Because the value Daniel Leo Stachelin is stored as a single string, the subscript [1] displays the whole string. Subscripts start at one.

  4. The string consists of one word. There are not two words, so by using a subscript of [2], the shell complains that the Subscript is out of range.

  5. To create a wordlist, the string is enclosed in parentheses. An array is created. The string is broken up into a list of words and assigned to the variable name.

  6. The first element of the array is printed.

  7. The second and third elements of the array are printed.

  8. The user is asked for input.

  9. The Linux head command with a numeric option of 1 will take one line of input. (If -2 were given as an option, two lines of input would be read). The head command is in backquotes causing the shell to perform command substitution, i.e., return the output of the head command and assign it to the variable, city. The user's input will be returned. When command substitution is performed, the returned values are stored as a list or array of space-separated strings. The variable city is an array. Chico is the first element of the array.

  10. The first element of the city array is printed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.5.57