Jump to content
Raffie77

[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
sourceFiles="/Users/$USER/temp/"

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 ;;
esac
set +x

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

-geotag=$tracklog

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?

Share this post


Link to post
Posted (edited)

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

Edited by vitor
Fixed typo: if → it

Share this post


Link to post
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

 

 

Share this post


Link to post
Posted (edited)
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

Share this post


Link to post
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

Share this post


Link to post
Posted (edited)

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

Share this post


Link to post
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

 

Share this post


Link to post

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).

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
1 hour ago, Raffie77 said:

$tracklog is set with 


-userparam tracklog="$clip"

 

No, it isn’t. That’s setting the program argument tracklog, not the shell variable $tracklog, which is what you were trying to pass to the -geotag flag.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...