10.4 Interactive Shell Programs

   

Interactive shell programs can read user input at run time with the help of the read command. Most of the time you will use the echo command to display a message before the read command is executed. This message informs the user of what the program is expecting. These programs are used in situations where a program first checks some system parameter and then requires user input to perform an operation on it. As an example, if you want to talk to another user on the system, you may first want to see who is logged into the system. After getting a list of users, you may initiate conversation with a particular user using the talk command.

The read Command

The read command takes one line of input from the user and assigns it to a variable. The variable name is provided as an argument to the read command. After entering some text, the user presses the graphics/enter.gif key. Below is script-07 , which lists all users currently logged into the system and then waits for you to enter a user name. After getting the user name, it initiates conversation with that user using the talk command.

 $  cat script-07  #!/usr/bin/sh echo "Currently logged in users are:" who echo echo echo "Enter the name of the user to whom you want to talk" read NAME echo "initiating talk with $NAME" talk $NAME $ 

After you select a user, the program rings the other party and displays a message asking the other user to respond to your talk request. If that user accepts your request, a talk window appears. Before the talk window appears, the program executes as shown below. Here you initiate a talk session with a user linda .

 $  ./script-07  Currently logged in users are: boota       pts/t0       Oct 18 17:53 linda       pts/0        Oct 18 22:13 Enter the name of the user to whom you want to talk linda <The talk window appears which covers the full screen> 

The echo Command

You have already used the echo command to display text on your screen. This command uses escape characters that can be used to format the displayed text to enhance its readability. The escape characters used with the echo command are listed in Table 10-2.

Table 10-2. Escape Characters Used with the echo Command
Character Effect
\a Alert character (beep)
\b Backspace
\c Suppress new line at the end of displayed text
\f Form feed
\n Insert a new line character
\r Carriage return
\t Insert a tab character
\\ Backslash
\ nnn Character having ASCII value nnn in the octal format. The first n is 0.

In the example of script-07 , the cursor goes to the next line after the message, " Enter the name of the user to whom you want to talk ". If you want the cursor to stay in the same line until you enter the user name, you need to suppress the new line character by using \c in the echo command as follows .

 echo "Enter user name to whom you want to talk \c" 

You can also use \a to add a beep to your program as soon as this command is executed.


   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

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