-1

I am trying to check:

if $file is a binary, and the $file is not an image file then do something.

if [[ "$(file --dereference --mime "$FILE")" =~ binary ]] && [[ "$FILE" != \.jpg$|\.jpeg$|\.png$ ]]; then
  echo "$1 is a binary file"
  exit 0
fi

The error is a syntax error in conditional expression: unexpected token

I guess I am probably overlooking something simple. I have googled quite a bit but cannot get a working statement. Any tips are greatly appreciated.

  • 2
    `!=` is a plain (/glob pattern) string comparison, not a regex comparison. Unfortunately, there's no negative form of `=~`, but you can always just use `[[ ! "$FILE" =~ ...` BTW, I find the regex pattern `\.(jpg|jpeg|png)$` easier to read (and equivalent). – Gordon Davisson Jun 16 '19 at 04:02
  • See: [Negate if condition in bash script](https://stackoverflow.com/q/26475358/3776858) – Cyrus Jun 16 '19 at 06:09

1 Answers1

1

You seem to trying to do a negate match on the second [[. You can do it by putting ! before a match =~

Here is an example that may help you:

[[ ! 'foo.png' =~ \.(jpe?g|png)$ ]] && echo not a image
geckos
  • 5,687
  • 1
  • 41
  • 53
  • 1
    Beware of the double negative. You test for `not an image`, so when the test succeeds, use `&&` (not `||`) before printing `echo "not an image"` – Walter A Jun 16 '19 at 12:31