2

I've created a bash script to automate editing the EXIF data in image files. I can't figure out how to get the script to issue a command with the * wildcard (asterisk) to specify a file name pattern.

The script properly assembles a CLI command that executes properly if I enter it manually outside of the bash script. But from within bash, the script returns a file not found error.

Here's my code:

#!/bin/bash

# Usage: Place this script in the directory of jpg files whose EXIF data is to be edited.

echo Press enter to accept default values.

# Enter camera manufacturer.
default="Pentax"
read -p "Enter camera manufacturer [$default]: " cameramake
cameramake=${cameramake:-$default}
exifargs="-model=$cameramake"

# Enter camera model.
default="K1000"
read -p "Enter camera model [$default]: " cameramodel
cameramodel=${cameramodel:-$default}
exifargs="$exifargs -make=$cameramodel"

# Enter slide film type.
default="Kodachrome slide film."
read -p "Enter slide film type [$default]: " filmtype
filmtype=${filmtype:-$default}
exifargs="$exifargs -imagedescription='PIE PS-5000 slide scanner. VueScanner 32 scanning softare. $filmtype'"

# Enter file name pattern.
default="*.jpg"
read -p "Enter file name pattern [$default]: " namepattern
namepattern=${namepattern:-$default}
exifargs="$exifargs $default"

#Use exiftool to edit EXIF data
echo /home/richard/Dropbox/Pictures/Tools/ExifTool/exiftool "$exifargs"
/home/richard/Dropbox/Pictures/Tools/ExifTool/exiftool "$exifargs"

I inserted the "echo" command to check that my command is correct. It is (at leats for the CLI). The results of running the script is:

richard@richard-laptop:~/Dropbox/Pictures/Photos/test$ sh exifbatch.sh
Press enter to accept default values.
Enter camera manufacturer [Pentax]: 
Enter camera model [K1000]: 
Enter slide film type [Kodachrome slide film.]: 
Enter file name pattern [*.jpg]: 
/home/richard/Dropbox/Pictures/Tools/ExifTool/exiftool -model=Pentax -make=K1000 -imagedescription='PIE PS-5000 slide scanner. VueScanner 32 scanning softare. Kodachrome slide film.' *.jpg
No file specified

But when I enter the exact same text as create by the script on the command line, it functions as I want, finding the files in the current directory that match the name pattern. (The warnings thrown by exiftool are expected.):

richard@richard-laptop:~/Dropbox/Pictures/Photos/test$ /home/richard/Dropbox/Pictures/Tools/ExifTool/exiftool -model=Pentax -make=K1000 -imagedescription='PIE PS-5000 slide scanner. VueScanner 32 scanning softare. Kodachrome slide film.' *.jpg
Warning: [minor] Ignored empty rdf:Seq list for darktable:mask_id - 1987_California-01.jpg
Warning: [minor] Ignored empty rdf:Seq list for darktable:mask_id - 1987_California-02.jpg
2 image files updated

How do I change my basch script so that the command will executed as it does on the CLI? I think there's something wrong with how I'm using the * character. If I don't put quotation marks (") around the $exifargs variable name, bash immediately interprets the *.jpg and appends all the file names into the string, which then confounds the exiftool command.

Thanks!

  • What do you mean by "which then confounds the exiftool command"? – choroba Oct 23 '16 at 21:23
2

Quoting in shell is tricky. If you want to use several parameters, especially when some of them contain whitespace, use arrays. Note that you need real bash to run the script to use arrays, so don't start the script with sh, which runs dash instead, with no array support.

args=( -model="$cameramake"
       -make="$cameramodel"
       -imagedescription="PIE PS-5000 slide scanner. VueScanner 32 scanning softare. $filmtype"
       $namepattern )
exiftool "${args[@]}"

BTW, are you sure you want to name the variable holding the model "make" and naming the variable holding the maker "model"? Also note that "software" has a w after t.

  • That worked like a dream! You da man (or da woman, but your avatar sure looks beardy for a woman...)! – trinkner Oct 23 '16 at 23:01
  • BTW @choroba, does the [@] instruct bash to use all arguments inside the array? – trinkner Oct 23 '16 at 23:02
  • @trinkner: Yes, it does. Try searching around man bash for details. – choroba Oct 24 '16 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.