3

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
  • 204,365
  • 48
  • 270
  • 300
Lukap
  • 31,523
  • 64
  • 157
  • 244

2 Answers2

10

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
  • 238,783
  • 38
  • 220
  • 352
2

Why not use a loop ?

for i in file.*; do
   if [[ -e $i ]]; then
      # exists...
   fi
done
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440