0

This is my script:

#!/bin/bash
set -x
arguments="some explanation"

if [[ $1 == -f ]]; then
    exiftool '-FileName<DateTimeOriginal' -d "%Y-%m-%d %H,%M,%S.%%e" "$2"
elif [[ $1 == -d ]]; then
    exiftool -d %Y-%m "-directory<datetimeoriginal" "$2"
elif [[ $1 == -fd ]]; then
    exiftool '-FileName<DateTimeOriginal' -d "%Y-%m-%d %H,%M,%S.%%e" "$2"
    exiftool -d %Y-%m "-directory<datetimeoriginal" "$2"
elif [[ $1 == -p ]]; then
    exiftool '-FileName<DateTimeOriginal' -d "%Y-%m-%d.%%e" "$2"
else
    echo -e $arguments
fi
set +x

If I run exifrename -f . it renames all files in the current folder. But let's say I have the following 3 files:

IMAG01234.jpg
IMAG01235.jpg
Ralph at home.jpg

and with them I run `exifrename -f IMAG*. It only renames one IMAG file.

When I debug this script, I see this:

+ [[ -d == -f ]]
+ [[ -d == -d ]]
+ exiftool -d %Y-%m '-directory<datetimeoriginal' IMAG01235.jpg

What can I do make the last line say IMAG* and not IMAG01235?

Rene Knop
  • 1,788
  • 3
  • 15
  • 27
Ralph Schipper
  • 701
  • 2
  • 12
  • 24
  • The shell expands the wildcard before calling exifrename, so it's getting called with several parameters. Maybe use `"$@"` (with a `shift`) instead of `$2`? – choroba Sep 18 '18 at 11:46

1 Answers1

1

When you are doing IMAG*, then the bash shell does globbing and expands IMAG* to 2 files and passes them as arguments to your script. You need to iterate through all the command line arguments:

Example:

#!/bin/bash

option="$1"  # this contains -f, -d, or something.
shift 1

for filename in "$@"; do
    ls -al "$filename" # Just an example. But you get the point.
done

In your case, you could do the following assuming, that there would be always be one option and followed by list of files.

option="$1"
shift 1
for filename in "$@"; do
    if [[ "$option" == '-f' ]]; then
        exiftool '-FileName<DateTimeOriginal' -d "%Y-%m-%d %H,%M,%S.%%e" "$filename"
    elif [[ "$option" == '-d' ]]; then
        exiftool -d %Y-%m "-directory<datetimeoriginal" "$filename"
    elif [[ "$option" == '-fd' ]]; then
        exiftool '-FileName<DateTimeOriginal' -d "%Y-%m-%d %H,%M,%S.%%e" "$filename"
        exiftool -d %Y-%m "-directory<datetimeoriginal" "$filename"
    elif [[ "$option" == '-p' ]]; then
        exiftool '-FileName<DateTimeOriginal' -d "%Y-%m-%d.%%e" "$filename"
    else
        echo -e $arguments
    fi
done

Note: If you comfortable with bash scripting, it is more readable to do with case/esac instead of bunch of if/else.

apatniv
  • 1,771
  • 10
  • 13