ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: richardl152 on November 13, 2020, 01:12:15 PM

Title: bash input variables into exiftool meta tag booleans
Post by: richardl152 on November 13, 2020, 01:12:15 PM
Hi Phil - apologies if a FAQ,  but I am having real problems getting this syntax to work for three bash functions.     The three are basically recursively searching for images within a GPS tolerance of location and also by time band.   They work for hard coded values but not for bash input variables on a Mac.

Grateful if you could take a look.   Its driving me nuts as Exiftool  reports use of uninitialized value $info{"3"} and no idea why.

An example invocation is


exiftool_recursive_copy_image_by_date test 2019:01:01 2020:12:31


exiftool_recursive_copy_image_by_gps () {
echo "search_name latitude longitude margin 1e-4"
mkdir -p $HOME/TEST/"$1"
exiftool -v  -progress  -if 'abs($gpslatitude# - "$2") < 1e-4 and abs($gpslongitude# - "$3") < "$4"' -o $HOME/TEST/"$1"  -r .

}
export -f exiftool_recursive_copy_image_by_gps


exiftool_recursive_copy_image_by_gps_and_date () {
echo "search_name latitude longitude margin eg 1e-4 greater than date eg 2019:06:01 and 2020:12:31"
mkdir -p $HOME/TEST/"$1"

exiftool  -v -progress -if 'abs($gpslatitude# - "$2") < 1e-4 and abs($gpslongitude# - "$3") < "$4" and $dateTimeOriginal gt "$5" and $dateTimeOriginal lt "$6" ' \
-o $HOME/TEST/"$1"   -r .

}

export -f exiftool_recursive_copy_image_by_gps_and_date


exiftool_recursive_copy_image_by_date () {
echo "search_name then greater than date eg 2019:06:01 and 2020:12:31"
mkdir -p $HOME/TEST/"$1"

exiftool -v -progress  -if '($dateTimeOriginal gt "$2") and ($dateTimeOriginal lt "$3") ' -o $HOME/TEST/"$1"   -r .

}

export -f exiftool_recursive_copy_image_by_date

Title: Re: bash input variables into exiftool meta tag booleans
Post by: StarGeek on November 13, 2020, 01:35:14 PM
Bash isn't my strong suit, but I believe the bash variables should be separate from the single quoted parts.  For example, something like this
'abs($gpslatitude# - '"$2"') < 1e-4 and abs($gpslongitude# - '"$3"') < '"$4"

I could be wrong though.
Title: Re: bash input variables into exiftool meta tag booleans
Post by: richardl152 on November 13, 2020, 01:59:48 PM
Many thanks but sadly not - getting syntax error now:



exiftool_recursive_copy_image_by_date () {
echo "search_name then greater than date eg 2019:06:01 and less than date"
mkdir -p $HOME/TEST/"$1"
exiftool -v -progress  -if '($dateTimeOriginal gt '"$2"') and ($dateTimeOriginal lt '"$3"') ' -o $HOME/TEST/"$1"   -r .
}
export -f exiftool_recursive_copy_image_by_date




Title: Re: bash input variables into exiftool meta tag booleans
Post by: richardl152 on November 14, 2020, 05:31:24 AM
as an update:

exiftool -v -progress  -if '($createdate# gt "2019:01:01") and ($createdate# lt "2020:12:31") ' -o $HOME/TEMP/"$1"   -r .

as a syntax works in a script,  but passing the same values through as $2 and $3 arguments (even enclosing in "" or ''  at the invocation) does not

exiftool -v -progress  -if '($createdate# gt '"$2"') and ($createdate# lt '"$3"') ' -o $HOME/TEMP/.  -r .

Grateful for any help.  I know I'm doing something stupid but running out of combinations of what to try.
Title: Re: bash input variables into exiftool meta tag booleans
Post by: richardl152 on November 14, 2020, 08:28:43 AM
Solved it !! very obscure but...


exiftool -v -progress  -if '($createdate# gt '\"$2\"') and ($createdate# lt '\"$3\"') ' -o $HOME/TEST/"$1"   -r .



you have to escape the double quotes around the shell variables....   
Title: Re: bash input variables into exiftool meta tag booleans
Post by: StarGeek on November 14, 2020, 11:13:26 AM
Ah, yes, I see my mistake.  I wasn't paying attention to the full command and blatantly ignored the gt and lt of your command, which would require quotes.

Out of curiosity, try testing this out to see if it works without having to escape the double quotes
'($createdate# gt "'$2'") and ($createdate# lt "'$3'")'
Title: Re: bash input variables into exiftool meta tag booleans
Post by: richardl152 on November 14, 2020, 01:16:44 PM
(thanks I will check).    In the hope it helps others,  this is the working stand along bash script of the most complex of the expressions from which the others are simpler variants.


#!/bin/bash                                                                                                                                                                               

echo "search_name latitude longitude margin 1e-4 and eg dates 2019:01:01 2020:12:31"

mkdir -p $HOME/TEST/"$1"


exiftool -v  -progress  -if \
         '($createdate# gt '\"$5\"') and ($createdate# lt '\"$6\"')  and  \                                                                                                               
         abs($gpslatitude# - '\"$2\"') < '\"$4\"' and abs($gpslongitude# - '\"$3\"') < '\"$4\"' ' \
         -o $HOME/TEST/"$1"  -r .





Title: Re: bash input variables into exiftool meta tag booleans
Post by: richardl152 on November 14, 2020, 01:26:17 PM
Quote from: StarGeek on November 14, 2020, 11:13:26 AM
Ah, yes, I see my mistake.  I wasn't paying attention to the full command and blatantly ignored the gt and lt of your command, which would require quotes.

Out of curiosity, try testing this out to see if it works without having to escape the double quotes
'($createdate# gt "'$2'") and ($createdate# lt "'$3'")'

Yes, you are right and it results in much cleaner working code,  thanks!!


#!/bin/bash                                                                                                                                                                               
echo "search_name latitude longitude margin 1e-4 and eg dates 2019:01:01 2020:12:31"

mkdir -p $HOME/TEST/"$1"
exiftool -v  -progress  -if \
         '($createdate# gt "'$5'") and ($createdate# lt "'$6'")  and  \                                                                                                                   
         abs($gpslatitude# - "'$2'") < "'$4'" and abs($gpslongitude# - "'$3'") < "'$4'" ' \
         -o $HOME/TEST/"$1"  -r .




Title: Re: bash input variables into exiftool meta tag booleans
Post by: richardl152 on November 21, 2020, 01:01:40 PM
My code for GPS comparison refuses to identify correct GPS coordinates (checked with Exiftool).    I must be doing something wrong (Mac) but can't see it.  Grateful if there's anything obviously wrong with the lat/long abs range check.  Did I use the correct tag processing?




#!/bin/bash                                                                                                                                                                                                                                           

mkdir -p  "$HOME/TEMP/GPS_WIDE_""$1""_""$2"

echo "input latitude and longitude" ;

if [ "$#" -ne 2 ]; then
    echo >&2 'Expected two arguments'
    exit 1
fi

exiftool  -if 'abs($GPSLatitude# - "$1") < 1e-2 and abs($GPSLongitude# - "$2") < 1e-2' -o  "/$HOME/TEMP/GPS_WIDE_$1""_""$2" -r .
# I also tried
# exiftool  -if 'abs($gpslatitude# - "$1") < 1e-2 and abs($gpslongitude# - "$2") < 1e-2' -o  "/$HOME/TEMP/GPS_WIDE_$1""_""$2" -r .

Title: Re: bash input variables into exiftool meta tag booleans
Post by: StarGeek on November 21, 2020, 02:14:19 PM
Shouldn't that be
$GPSLatitude# - "'$1'"
and
$GPSLongitude# - "'$2'"
?
Title: Re: bash input variables into exiftool meta tag booleans
Post by: richardl152 on November 21, 2020, 05:26:56 PM
yes !!  you are absolutely right, this keeps catching me out,  thank you for your help,  greatly appreciated