2

I have this

if [[ -e file.jpg ]] ;then echo "aaaaaaaaa"; fi

and it prints "aaaaaaa"

but I want to print if there is file.png or file.png also

so I need something like this

if [[ -e file.* ]] ;then echo "aaaaaaaaa"; fi

but it doesn't work I am missing something in the syntax

Thanks

William Pursell
  • 190,037
  • 45
  • 260
  • 285
Lukap
  • 30,736
  • 61
  • 153
  • 243

2 Answers2

8

If you enable bash's nullglob setting, the pattern file.* will expand to an empty string if there are no such files:

shopt -s nullglob
files=(file.*)
# now check the size of the array
if (( ${#files[@]} == 0 )); then
    echo "no such files"
else
    echo "at least one:"
    printf "aaaaaaaaa %s\n" "${files[@]}"
fi

If you do not enable nullglob, then files=(file.*) will result in an array with one element, the string "file.*"

glenn jackman
  • 223,850
  • 36
  • 205
  • 328
2

Why not use a loop ?

for i in file.*; do
   if [[ -e $i ]]; then
      # exists...
   fi
done
Brian Agnew
  • 261,477
  • 36
  • 323
  • 432