Jump to content

[SOLVED] Just one var doesn't work in bash script

Recommended Posts

It's very bizar I have this script in Alfred:

#Exiftool utility for Alfred workflow #
set -x
IFS=$'\t' read -ra files <<< "${file_list}"
command=${command} #Located at the arg block after the List Filter in the workflow

case $command in
	basRen) #Basic rename
		${exiftool} -d "%Y-%m-%d %H,%M,%S%%-c" '-filename<${DateTimeOriginal} $Make-$Model.%e' "${files[@]}"
	k_basRenExtFile) #Basic rename with an extra added name from a query
		${exiftool} -userparam extName="$keyword" -d "%Y-%m-%d %H,%M,%S%%-c" '-filename<${DateTimeOriginal} $Make-$Model $extName.%e' "${files[@]}"
	k_basRenCurFile) #Rename the current file(s) with a extra query e.g. test.jpg > test foo.jpg
		${exiftool} -userparam extName="$keyword" -d "%%f" '-filename<${DateTimeOriginal} $extName.%e' "${files[@]}"
	datMap) #Move selected files to a subfolder YYYY-mm created from DateTimeOriginal exif tag, if the folder doesn't exsist create it creates that folder
		${exiftool} -d "%%d/%Y-%m/%%f.%%e" '-filename<DateTimeOriginal' "${files[@]}"
	geoTag) #Select a single file and Show it in Apple maps or Google maps. In the main vars section choose by map_provider 'Apple' or 'Google'
		[[ "${map_provider}" == 'Google' ]] && map_url='https://www.google.com/maps?t=k&q=' || map_url='http://maps.apple.com/?q='
		coordinates="$(${exiftool} -printFormat '$gpslatitude, $gpslongitude' --printConv "${files}")"
		open "${map_url}${coordinates}"
	tracklog) #Find the tracklog in finder > double tab cmd and choose 'copy path to clipboard' and run this script
		${exiftool} -overwrite_original -userparam tracklog="$clip" -geotag=$tracklog "${files[@]}"
	photos2kml) #Select photos and check if it has GPS data in the Exif, if so that create a KML file and open it in Google Earth
		${exiftool} -p "${sourceFiles}gpx.fmt" -r "${files[@]}" > "${sourceFiles}photos.kml"
		open "${sourceFiles}"
		open "${sourceFiles}photos.kml"
	dateFields) #Show alle date fields in CSV format from selected files
		${exiftool} -csv -time:all -s  "${files[@]}" > "${sourceFiles}exiftool_output.csv"
		open "${sourceFiles}exiftool_output.csv" 
	megapixels) #Show magapixels in CSV format from selected files
		${exiftool} -csv -megapixels  "${files[@]}" > "${sourceFiles}exiftool_output.csv"
		open "${sourceFiles}exiftool_output.csv" 
	basDetails) #Show basic information in CSV format from selected files, you can edit the fiels in the format file
		${exiftool} -c %.7f -api filter='$_ = qq{"$_"} if s/"/""/g or /(^\s+|\s+$)/ or /[,\n\r]/' -f -p "${sourceFiles}csv.fmt" "${files[@]}" > "${sourceFiles}exiftool_output.csv"
		open "${sourceFiles}exiftool_output.csv" 
		exit ;;
set +x

All the case parts works perfect, except case tracklog the part that says:


when I run this script I see this result in the debug screen:

case $command in
+ /usr/local/bin/exiftool -overwrite_original -userparam 'tracklog=/Users/ralphschipper/Downloads/2020-05-09 17_15_25.gpx' -geotag= '/Volumes/Ext 1TB/Google Drive/temp/test1/MapA/P1110188.JPG'

if you look at the pard -geotag= you see its blank the next part is the photo that is selected tru a File Action 

As I said all the rest of the cases works great except tis part.


Can anyone see what I'm doing wrong here?

Link to comment
4 minutes ago, vitor said:

What’s $tracklog supposed to be? You’re not setting the variable anywhere, so the script is reading if for what it is: nothing.

oh sorry I forgot to say:

var tracklog is located in a var in alfred see screenshot385775140_Schermafbeelding2020-05-09om20_53_22.png.9f3b528ad259c45c01f2c64b79330f52.png


when I add this line in te script:

${exiftool} -overwrite_original -userparam tracklog="$clip" -geotag=$tracklog "${files[@]}"
echo "Clip = $clip"

The debug result =

Clip = /Users/ralphschipper/Downloads/2020-05-09 17_15_25.gpx

I have the workflow here



Link to comment
51 minutes ago, Raffie77 said:

var tracklog is located in a var in alfred see screenshot


There’s no $tracklog in that screenshot, nor anywhere else in the workflow. It’s like Vítor says: you’re using an unset variable.


Edited by deanishe
Link to comment
2 minutes ago, vitor said:

Try ${exiftool} -overwrite_original -geotag="${clip}" "${files[@]}"


ok this is a exiftool thing lets say if I want to see the model of  a image, I type:

 ${exiftool} $model photo.jpg

if I type:

${exiftool} -overwrite_original -geotag="${clip}" "${files[@]}"

exiftool thinks I want to see the property of ${clip} and that doesn't exist

that's where the -userparam kicks in

-userparam tracklog="$clip"

it means create a property tracklog with the value of $clip

and $clip means clipboard see the screenshot


and when I echo $clip I see this result in the debug:

Clip = /Users/ralphschipper/Downloads/2020-05-09 17_15_25.gpx

so the clip var is set correctly


I hope this makes sense

Link to comment

Yes, I understand that exiftool allows you to set your own parameters, but according to their own manual page -geotag takes a file path as input. User parameters do not take over shell variables, they just have (confusingly) similar syntaxes.

Edited by vitor
Link to comment
Just now, deanishe said:

I’m confused. Why are you talking about $clip? The problem is that $tracklog isn’t set, isn’t it?


$tracklog is set with 

-userparam tracklog="$clip"
-userparam          tracklog=                  "$clip"
^                   ^                          ^
exiftool function   create a exiftool value    defined in the workflow var it's clipboard content1


Link to comment
5 minutes ago, vitor said:

Again, try ${clip} directly, like the example I gave you. -geotag expects a file path (check man exiftool). You’re conflating bash variables with user parameters, which are meant to be used for tagging (again, manual).


huh?😣 that works

ok now I am confused, it's great that it works now thanks @vitor.

If you take a look at the script in my first post

you see a case k_basRenExtFile and k_basRenCurFile

those work. and I thought I used the same technique 


I'm gonna study this script for a while

thank you both for your time, I appreciate that very much

Link to comment
7 minutes ago, Raffie77 said:

huh?😣 that works

ok now I am confused


That’s natural. You’re confused because the syntax exiftool has chosen for user parameters is the same as bash variables, so it’s hard to distinguish which is being considered when. A different character for user parameters, like %, would have made the distinction clearer.


13 minutes ago, Raffie77 said:

and I thought I used the same technique 


The context was different. Above you’re using them as part of the exiftool instructions, but on the failing example it was a part of the shell’s input.

Here’s a way you can test, while you’re studying the script: bash variables do not expand inside single quotes ' (meaning they will be used literally, $ and all), but they do inside double quotes ". User parameters will be substituted inside single quotes. So use single quotes, and when you see something not expanding, you’ll know you’re dealing with a bash variable.

Link to comment
  • vitor changed the title to [SOLVED] Just one var doesn't work in bash script

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...