I am tweaking a script so that it will ask the user for a file of a specific type. If the file entered does not match the specific file type, it goes into a while loop and repeats until the user enters the correct file.
In this case, I'm asking the user for a .zip
file in:
read -p "Enter zip file: " package1
while [ package1 != *.zip ] ; do
read -p "Please enter file ending in '.zip': " package1
done <<< package1
echo $package1
I've had errors until this point. Now, I just get a blank cursor, no output. Whether I enter *.zip
or not, it proceeds to the next line with a blank cursor and no output.
Thoughts?
EDIT:
Alright, I've decided to go with the select
loop and it's a much better option than while
in this case.
But what if that zip
file (or any file-type prompted) could be in any other directory? I want it to give the option to change directories while in the select-loop
shell.
I tried so far:
echo Select zip file:
options=(".." *.zip)
select package1 in "${options[@]}" ; do
case $package1 in
1 ) cd ..; echo Select zip file: ;;
# Different Directory ) ;;
* ) break ;;
esac
done
But the loop won't re-display the menu items after changing directories. I am not sure how to offer the user to cd
to a totally different path other than ../
if they want, nor what to use for
"All other existing \# file items" ) ... ; break;;
inside of case
.zip
at the end of the filename. Since you explicitly require a ZIP file, why not prompt for a regular filename, and then have the script append the.zip
? That seems more user-friendly to me. YMMV.*.zip
or use filename completion? Having a user type in a pathname is just a sure way to frustrate the user, as they can't correct what they type after pressing Enter.