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