0

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
Matt
  • 23
  • 2
  • 7

1 Answers1

1

Test supports using wildcards, so [ -f *.gz ] will be true if there is one file matching *.gz (and false otherwise - as @tripleee pointed out, this will fail with multiple matches; see below for a better variant).

Another possibility is to use if ls *.gz &>/dev/null; then ...; else ...; fi (which will not throw any errors).

GuiltyDolphin
  • 768
  • 1
  • 7
  • 10