Matt Matt - 6 months ago 27
Bash Question

Need to check if .gz files exist in a directory

I have a prompt in my shell script where the user can choose a directory. I am trying to make it so that if a .gz file exists in that directory, they exit the loop, and if not, the user is asked again to choose a directory, but it just isn't working. Here's what I have so far:

ls -d */
while :
do
echo "Which Directory do you want to access?"
read input_variable1
cd $input_variable1
if [ CHECK FOR .gz ]
then
break
else
ls -d */
echo "no .gz files to unzip. Try again."
fi
done

Answer

Test supports using wildcards, so [ -f *.gz ] will be true if there are any files matching *.gz (and false otherwise).