Rename using shell variable substitution

Started by jpoet, September 26, 2016, 05:49:58 PM

Previous topic - Next topic

jpoet

Hi,

I am trying to create a bash script to rename my files.  It will take a couple of arguments which will be used as part of the file name.  I have tried this:


#!/bin/bash

TRIP=$1

PLACE=$2

if [ "X$TRIP" = "X" ]
then
    echo "TRIP must be provided"
    exit 1
fi

if [ "X$PLACE" = "X" ]
then
    echo "PLACE must be provided"
    exit 1
fi

FORMAT="/home/john/Pictures/${TRIP}-%Y/%Y%m%d-%H%M%S"
exiftool -r -o . -d $FORMAT '-filename<${DateTimeOriginal}${SubSecTimeOriginal}-${fileindex;$_=sprintf("%.5d",$_)}%+2c-${PLACE}-${artist;tr/ /_/}.%e'  .


Using ${TRIP} as part of the format works.  Having ${PLACE} as part of the 'filename<..." fails with:
Quote
Warning: [minor] Tag 'PLACE' not defined - <filename>
Warning: No writable tags set from <filename>
    0 image files updated

I tried escaping ${PLACE} in every way I could think of, but cannot get it to work.  Can someone please tell me the correct format?

Thanks!

Phil Harvey

Shell scripts interpolate variables in double quotes, not single quotes.  So you need the to use double quotes for the shell variable.  Maybe something like this:

exiftool -r -o . -d $FORMAT '-filename<${DateTimeOriginal}${SubSecTimeOriginal}-${fileindex;$_=sprintf("%.5d",$_)}%+2c-'"${PLACE}"'-${artist;tr/ /_/}.%e'  .

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

jpoet

Hi Phil,

I had tried
"${PLACE}"
but I had not tried
'"${PLACE}"'

Since the whole
'-filename< ... '
Was in single quotes, I assumed that having single quotes in the middle would break something.  That is what I get for assuming.

Your example worked.  Thanks!

Phil Harvey

All I did was change quotes from single to double then back again.

'abc'"abc"'abc' is just the concatenation of three strings with different quoting.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).