#! /bin/sh

# This is a more advanced version of readloop.  We only want to know whether
# grep found the word, the word was not in the file, or the file could not be 
# read. We need to use the exit value from grep to do this.

# NOTE: this version does not work.  Why?

# Read two words from a line
read arg1 arg2

# Keep going until $arg1 doesn't contain anthing 

while [ "$arg1" != "" ]  
do
    echo "Looking for \"$arg1\" in file $arg2:"
    grep $arg1 $arg2 > /dev/null 2>&1   
                       # map file descriptor 2 to file descriptor 1
#    grep $arg1 $arg2 > /dev/null 2> errors

    # $? holds the exit value of the previously executed command.
    echo $?
    if [ $? -eq 0 ]
    then
	echo "Found \"$arg1\" in $arg2"
    elif [ $? -eq 1 ]
    then
	echo "\"$arg1\" does not appear in $arg2"
    elif [ $? -eq 2 ]
    then
	echo "Could not read $arg2"
    fi
    echo --------------------

    read arg1 arg2
done
