I'm working on a bit of homework and I don't need someone to do it for me but to help out with this portion. The rest of it is pretty easy to manage since I already have it mostly done but right now I can't figure out how to get the first part to work.

The problem is to ask the user for the name of the file, verify that the file exists and is readable, and if the file does not exist or not readable to issue an error message. I think I have most of it down but I'm just confused how to get the user input to be checked. Basically the error messages and the fact that it is readable or a file just get skipped and the script goes to the sorting part of the assignment. Any ideas? My script is pasted below.

#       The purpose of this script is to sort a
#       file containing a single line address
#       by the zip code, last name, and first
#       name and then format it properly.
#
echo "Please enter a file to sort: \c"
read filename

for file in $1
do
        if test -f $file
                then
                echo "$file is a file."
                else
                echo "$file is not a file."
        fi
        if test -r $file
                then
                echo "$file is readable"
                else
                echo "$file is not readable."
        fi
done

echo "Please enter one of four of the follow selections:\n"
echo "1 to sort by zip code."
echo "2 to sort by last name."
echo "3 to sort by first name."
read choice

        case $choice in
                1) sort -k8 $1
                exit;;
                2) sort -k1 $1
                exit;;
                3) sort -k2 $1
                exit;;
                *) echo "Please enter a valid choice."
        esac

For anyone that's curious about the solution I figured it out after hours of playing around.

if test -f $filename
        then
        echo "$filename is a file."
        else
        echo "$filename is not a file, please restart."
        exit
fi
if test -r $filename
        then
        echo "$filename is readable"
        else
        echo "$filename is not readable, please restart."
        exit
fi

I just wish I could figure out how to get the script to replay if the file is not a file or is not readable. Any ideas?

Good work,

I noticed that while I read your first post, but you found that read actually sets the variable for you. Good deal :)

As for replaying, you can either loop by setting a trapping variable in a while loop (the cumbersome way) like:

goahead=0
while [ $goahead -ne 1 ]
do
     echo "Please enter a file to sort: \c"
     read filename
     if test -f $filename
             then
             echo "$filename is a file."
             goahead=1
             else
             echo "$filename is not a file, please try again."
     fi
done

or do it the simple way (for my way of thinking, anyway ;) and just exec your program from within, like:

if test -f $filename
        then
        echo "$filename is a file."
        else
        echo "$filename is not a file, please try again."
        exec $0
fi

There are definitely more than a few more ways to get around the issue (some much more elegant I'm sure ;)

Best of luck to you,

Mike

OR you could break it up into functions!

Make each part of the script into a function, and you can go back to whatever function you want at any time.

For example, you can turn that initial prompt into a function:

prompt () {
echo "Please enter a file to sort: \c"
read filename
}

Then you can go back to that function by calling "prompt", say for instance in place of "exit" if the file test or read test fails.

I hope this helps!
-G

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.