1
if [[ $1 == *.(png|jpg) ]]

is what I've tried, and it doesn't work. I need to check if $1 matches one of those filename suffixes, so I can then do stuff with it. I'm not sure how to go about this. I'm just getting into bash. I've searched all through these forums, and others. I have yet to find something that works for a conditional if statement.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
memespace
  • 21
  • 3

1 Answers1

3

@(this|that) matches this or that in Ksh-style extended globs that Bash also supports. (With Bash, you'd need to use shopt extglob to enable extended globs in other contexts[[ ]].)

if [[ $1 == *.@(jpg|png) ]]; then 
    echo match
else
    echo no match
fi

In Zsh, you could use *.(jpg|png) though.

And in standard sh you could use case, but would have to repeat the whole pattern:

case $1 in
    *.png|*.jpg) echo match;;
    *)           echo no match;;
esac

See:

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
  • @steeldriver, yes indeed, I've heard that too, just can't seem to remember it though... – ilkkachu Mar 24 '21 at 23:56
  • perfect, thanks. Just one more question though, I've heard that using filename suffixes is unreliable, what would be the better option? Running `FILE` on each file to determine its type then categorize them from there? Or is this solution fine? – memespace Mar 25 '21 at 16:33
  • @memespace, well, sure it 's not perfect, since if you do `mv foo.jpg foo.png`, the suffix doesn't match the contents, which might confuse some programs. But it's still by far the most widely used way for telling the file type (I guess you could use extended attributes or such, but they're not portable.) And knowing the file type before trying to interpret it is just useful, in the least it saves time. Also, if you have the same file in different formats, you'd need to figure out some other way to tell the files apart (think `prog.c`, `prog.o` and `prog`; or `somedoc.odt`, `somedoc.pdf` etc.) – ilkkachu Mar 25 '21 at 16:38