How To properly break apart long lines of code...

Started by captured, September 15, 2018, 11:09:35 AM

Previous topic - Next topic

captured

I have a very long line of exiftool code.
What is the proper method of breaking into multiple lines to make more manageable ?
Thank you.

exiftool -t -q -q -m -p '$filename; $filetype; $mimetype; $imagewidth; $imageheight' -if '-e $filepath && -f $filepath && -s $filepath && $mimetype =~ m"^image/[^x]" && $filetypeextension !~ m"\.(?:NEF|RAF|ARW|CR2)$"i && $filetype =~ m"(JPEG|TIFF|PNG|WEBP|PSD|GIF)"i' .

I have this so far...
exiftool -t -q -q -m -p \
'$filename; $filetype; $mimetype; $imagewidth; $imageheight' \
-if '-e $filepath && -f $filepath && -s $filepath && $mimetype =~ m"^image/[^x]" && \
$filetypeextension !~ m"\.(?:NEF|RAF|ARW|CR2)$"i && $filetype =~ m"(JPEG|TIFF|PNG|WEBP|PSD|GIF)"i' .


Thank you.

System;
-Linux Mint 18.3 64bit Cinnamon.
-exiftool 11.10
-bash 4.3.48

Hayo Baan

You could use \ to move parts to separate lines, but still have everything behave as a single command. e.g.
exiftool -t -q -q -m -p '$filename; $filetype; $mimetype; $imagewidth; $imageheight' \
  -if '-e $filepath && -f $filepath && -s $filepath && $mimetype =~ m"^image/[^x]" && $filetypeextension !~ m"\.(?:NEF|RAF|ARW|CR2)$"i && $filetype =~ m"(JPEG|TIFF|PNG|WEBP|PSD|GIF)"i' \
  .

(note: you can skip testing both -e and -f; -f alone is enough to test for file presence.)

But, I think the best solution would be to make use of an arg file (-@ option)

For this your command it would look like this:

-t
-q
-q
-m
-p
$filename; $filetype; $mimetype; $imagewidth; $imageheight
-if
-e $filepath && -f $filepath && -s $filepath && $mimetype =~ m"^image/[^x]" && $filetypeextension !~ m"\.(?:NEF|RAF|ARW|CR2)$"i && $filetype =~ m"(JPEG|TIFF|PNG|WEBP|PSD|GIF)"i

(again, -e and -fcan be replaced with -f)

You then use this argument file like so:
exiftool -@ arg_file .
(note: you could even include the current directory . argument in the arguments file, but this is less flexible)

Hope this helps
Hayo Baan – Photography
Web: www.hayobaan.nl

captured

Thank you for your assistance Hayo Baan.
I'll keep the -@ solution for reference.

I have deliberately avoided using external files.

May I ask if it is possible to break apart the -if line, it is too long for me as is.
Can't I use another '\' (somewhere) ?

Best regards.

Hayo Baan

No you really can't, the if requires its argument to be a single argument and you can't split it with a \ since that needs to be outside the quotes, forcing it to be separate arguments.

However, what is possible is to simply split the string that forms the argument to the if:
exiftool -t -q -q -m -p '$filename; $filetype; $mimetype; $imagewidth; $imageheight' \
  -if '-e $filepath && -f $filepath && -s $filepath
   && $mimetype =~ m"^image/[^x]"
   && $filetypeextension !~ m"\.(?:NEF|RAF|ARW|CR2)$"i
   && $filetype =~ m"(JPEG|TIFF|PNG|WEBP|PSD|GIF)"i' \
  .
Hayo Baan – Photography
Web: www.hayobaan.nl

captured