1

I'm writing a bash script to do something if the directory has files of extensions: jpg, jpeg or png. But I'm screwing up when I try to check this with the for or while loops.

while [ filename in $IMAGE_DIR/*.jp*g -o filename in $IMAGE_DIR/*.png ] ; do
        # do something here

But I receive compile error:

line 30: [: too many arguments

I tried the following loops to no avail.

for (filename in $IMAGE_DIR/*.jp*g) || (filename in $IMAGE_DIR/*.png); do
while [ filename in $IMAGE_DIR/*.jp*g ] || [ filename in $IMAGE_DIR/*.png ] ; do
while [[ filename in $IMAGE_DIR/*.jp*g ]] || [[ filename in $IMAGE_DIR/*.png ]] ; do
while [[ filename in $IMAGE_DIR/*.jp*g ] || [ filename in $IMAGE_DIR/*.png ]] ; do
while (( filename in $IMAGE_DIR/*.jp*g )) || (( filename in $IMAGE_DIR/*.png )) ; do
while ( filename in $IMAGE_DIR/*.jp*g )) || (( filename in $IMAGE_DIR/*.png ) ; do

What am I missing here?

3
  • 1
    Why don't you do_something *.jpg *.jpeg *.png if there aren't any, you will do_nothing. Or use find -name "*.jpeg" or any other pattern you want for their name, and xargs them to your job. There is no reason to check all the filenames around, the shell offers you these files with one command or one glob. Commented Sep 25, 2020 at 16:14
  • You want to do something if the directory has files of extensions or do you want to iterate over these files? If this a duplicate of stackoverflow.com/questions/6363441/… ? Commented Sep 25, 2020 at 17:16
  • @KamilCuk: just want to iterate over these files. Commented Sep 25, 2020 at 18:50

2 Answers 2

3

Here is the way I believe it's easier to iterate multiple "types|extensions" of files. I hope with that you have an idea how to continue, you can create your if conditions inside that for loop.

$ ls -l
-rw-rw-r--  1 lucas lucas    0 Sep 25 13:58  test.jpeg
-rw-rw-r--  1 lucas lucas    0 Sep 25 13:58  test.jpg
-rw-rw-r--  1 lucas lucas    0 Sep 25 13:58  test.png
-rwxrwxr-x  1 lucas lucas   62 Sep 25 13:59  test.sh

$ cat test.sh 
#!/bin/bash
for i in *.jpg *jpeg *.png; do
  echo "hi $i"
done

$ ./test.sh 
hi test.jpg
hi test.jpeg
hi test.png
Sign up to request clarification or add additional context in comments.

3 Comments

echo "hi $i" would prevent problems with word splitting and special characters in filenames.
@BenjaminW. You're right. I will improve the sample, thank you
without shopt -s nullglob you will get the patterns when no files exists. I advise for ....; do if [ ! -e "$i" ]; then continue; fi; .... done
1

You are use readarray Instead of ".txt" just put any extension you want.

readarray FILES <<< "$(find . -name "*.txt")";
for file in ${FILES[@]}; do
    echo $file
done  

1 Comment

Isn't that just a more complicated way of writing for file in *.txt? It also descends into subdirectories, which may or may not be desired.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.