On 04/05/17 10:38, Tim Connors via luv-main wrote:
> On Tue, 2 May 2017, Andrew McGlashan wrote:
> 
>> # The ./ with the ls is there on purpose to make it safer
>> # from potential globbing issues
>> for old_fn in $(ls ./*.jpg)
> 
> Just stop it right now!  Stop doing that!
> 
> Just don't!
> 
> `for old_fn in *.jpg` is all you need, all you want.  Then make sure
> "$old_fn" is properly quoted elsewhere.

That's okay, so long as there is at least one target file, otherwise it
fails.

I've added a test before now and dropped the ls in the for.

There was another minor change, formatting the number to be 3 digits
with printf isn't necessary in the use case.


$ cat photo-wrk.sh
#!/bin/bash

set -u

create_some_files() {
        declare -a old_filenames
        old_filenames=(
        '10062005.jpg'
        '10062005(121).jpg'
        '10062005(122).jpg'
        '10062005(123).jpg'
        )

        for old_fn in "${old_filenames[@]}"
        do
                touch "${old_fn}"
        done
}

#create_some_files # for testing
#exit

ls ./*.jpg &>/dev/null || {
        echo no jpg files to process
        exit
}

# create new_names dir if it doesn't exist
[[ -d ./new_names ]] || {
        mkdir new_names
}

for old_fn in *.jpg
do

        # if filename has only 12 characters (including .jpg)
        # then it doesn't have a counter value, so give it 0
        if [[ ${#old_fn} -eq 12 ]]
        then
                counterx=000
        else
                # if it has a counter, it is supposed to already have 3 digits
                counterx=${old_fn:9:3}
        fi

        # Get date in seconds from epoc
        # - using old file name format of DDMMYYYY
        ab=$(date -d "${old_fn:4:4}-${old_fn:2:2}-${old_fn:0:2}" +%s)

        # Add day counter for photos in seconds
        ab=$((ab + counterx))

        new_fn=$(date +%Y%m%d -d "@${ab}")"(${counterx}).jpg"
        mv "${old_fn}" ./new_names/"${new_fn}"
        touch -d "@${ab}" "./new_names/${new_fn}"
done

ls -l --full-time "./new_names/"


Cheers
A.

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
luv-main mailing list
[email protected]
https://lists.luv.asn.au/cgi-bin/mailman/listinfo/luv-main

Reply via email to