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!

1
  • What do you mean by "which then confounds the exiftool command"?
    – choroba
    Commented Oct 23, 2016 at 21:23

1 Answer 1

3

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .