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 
1
  • This is not dup!! Commented Aug 10, 2017 at 14:11

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! Helped a lot
But it will fail with a syntax error if there is more than one matching file.
@tripleee True, I've updated the response to include a version that won't fail.
The command substitution is quite superfluous there. See also stackoverflow.com/questions/6363441/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.