DistributedNetworks DistributedNetworks


Unix Shell Scripts   «Prev 

Read Keyboard Input Command - Exercise

Using the read command to read keyboard input

Objective: Use the read command to collect user input in a script.

Exercise scoring

This exercise is worth 3 points.

Background/overview

You should have an account set up on the DistributedNetworks Labs server or on a UNIX server at your site. Log in to this account so you see a UNIX command line prompt. We will do this exercise with the Bourne shell.

Instructions

  1. Log in to your UNIX account so that you are working at a command prompt.
  2. At the command line, enter this command to start a Bourne shell, so that the commands used at the command prompt will match what you do in a Bourne shell script.

% sh

  1. At the command line, enter this command to read a line of user input:

$ read DOWNLOADFILE
  1. The cursor moves to the next line and waits. The prompt does not reappear. Type a filename (something that you make up is fine) and press Enter.
  2. The prompt returns but you see no other feedback. The variable, however, was set to the value that you entered. To see this, enter the following command at the command-line prompt:

$ echo $DOWNLOADFILE

  1. You see the value that you typed in during step 3.
  2. The dollar sign indicates to the echo command that the text is a variable name. Enter this command to see how echo responds without the dollar sign:
$ echo DOWNLOADFILE
  1. You see the word DOWNLOADFILE printed to the screen.