CSC128: Introduction to UNIX

User Interaction

Reading User Input

It is often helpful for a script to read user input.  To do this use the read command:
user@host:~$ read myvar
my dog has fleas

user@host:~$ echo $myvar
my dog has fleas

In this example, read takes a single line of user input  terminated with a newline, and places it in the variable specified,  myvar in this example.

If you specify more than one variable, read will place the first (whitespace-seperated) string in the first variable, the second in the second variable, etc..  The last variable gets all the remaining strings.  If more variables are specified than strings are provided, the extra variables will be empty.

By default, read will try to wait forever for the user to type in a line.  With the -t option, we can tell  read to wait only the specified number of seconds for the user to input a line, and then return if the time elapses.
user@host:~$ read -t 5 myvar
my dog has fleas

user@host:~$ echo $?

user@host:~$ read -t 1 myvar
my do

user@host:~$ echo $?

Note that if read returns before it has read a line from the user, it will return a value of 1, which evaluates as false.  This way, the script can test to see if it received a line in time, or if it timed out.

echo "Please enter a line."

until read -t 1 myvar
  echo "HA HA You're not fast enough!"

echo "Your text was $myvar"
This will continue to taunt the user until the user finishes entering a line ended with a newline.

See the man page for read for more options.


Many times, it is useful to test if a variable is equal to several different values, and execute different commands depending on which value it is equal to.


while [ $quitvar = no ]
    echo -n "converse> "
    read inputvar

    case $inputvar in
        hello )
            echo "Hello!";;
        goodbye )
            echo "Goodbye!"
            echo "So sad to see you go!"
        yes )
            echo "Are you sure?";;
        no )
            echo "Why not?";;
        * )
            echo "What would you like to talk about?";;
Note that the case construct is ended with esac .  Each possible match is followed by a right-parenthesis ( )), followed by commands to execute if it is a match.  The commands are terminated with a double semicolon (;; ). Then follows either another possible match or esac .


Print the file name of the terminal connected to standard input.

Change and print terminal line settings.

One of the most common uses of
stty is to change the current erase character.  Some terminals and terminal emulators send Ctrl-H, and others send Ctrl-?.  If you press backspace several times and see ^H^H^H^H , then the shell is expecting Ctrl-? to be backspace.  Change it to Ctrl-H by typing:
stty erase Ctrl-V Ctrl-H

This will appear as:

stty erase ^H

The Ctrl-V tells the shell to interpret the next control character literally.

find [path...] [options]
Search for files in a directory hierarchy.

This command will return a list of filenames that conform to the options specified.  By default, if no options are specified, it will return all files in the specified path and in all subdirectories.  Some options:
-maxdepth levels
This will restrict find to only the number of subdirectories specified (levels ).

-atime n
This will restrict find to only files that were last accessed n*24 hours ago.

-ctime n
This will restrict find to only files that were File's status was last changed n*24 hours ago.

-mtime n
This will restrict find to only files that were File's data was last modified n*24 hours ago.

-perm mode
This will restrict find to only files with permission set to exactly mode (octal or symbolic).  Symbolic modes use mode 0 (----------) as a point of departure.