Philipp HB
Philipp HB

Reputation: 179

Using variable in exiftool -if condition

I am trying to reorganise images based on keywords that are found in the IPTC metadata. More specifically, I need sort images into directories based on the species name in the subject pseudo tag of exiftool.

To do this, I have compiled the keywords in a .txt file (species_ls.txt), with each keyword on a new line, as such:

Asian Tortoise Banded Civet Banded Linsang ...

To sort the images I have created the following for loop, which iterates through each line of the document, with sed pulling out the keyword. Here, $line_no is the number of lines in the species_ls.txt file, and image_raw is the directory containing the images.

for i in 'seq 1 $line_no'; do
    sp_name=$(sed -n "${i}p" < species_ls.txt)
    exiftool -r -if '$subject=~/${sp_name}/i' \
        '-Filename=./${sp_dir}/%f%+c%E' image_raw`

Although the for loop runs, no conditions are being met in the -if flag in exiftool. I am assuming this is because the variable sp_name is not being passed into the condition properly.

Any suggestions, or a better way of doing this, would be appreciated.

Upvotes: 1

Views: 2150

Answers (1)

Tane
Tane

Reputation: 142

For the line with condition, rather than using single quotes (' '), it would be better to use double quotes (" ").

The single quotes mean that the content is passed literally, meaning your variable won't get expanded.

To overcome, the $subject line expanding (which I presume you don't want), you can just put a \ in front of the $ to escape it being read as a variable.

This line should now look like:

exiftool -r -if "\$subject=~/${sp_name}/i"

Hope this helps you!

Upvotes: 2

Related Questions