From eb884b43723710e0c126c07f40648831b42db261 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 2 Dec 2011 10:14:27 -0500 Subject: [PATCH] Add checks for required utilities to name-by-date.sh. --- posts/name-by-date/name-by-date.sh | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/posts/name-by-date/name-by-date.sh b/posts/name-by-date/name-by-date.sh index a4784bc..57feb2e 100755 --- a/posts/name-by-date/name-by-date.sh +++ b/posts/name-by-date/name-by-date.sh @@ -2,7 +2,7 @@ # # Rename JPEGs to reflect the creation date stored in their metadata. # -# Copyright (C) 2010 W. Trevor King +# Copyright (C) 2010-2011 W. Trevor King # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -20,36 +20,43 @@ # # # usage: name-by-date.sh $(find . -name '*.jpg') -# -# uses exif, [, cut, dirname, echo, file, grep, let, mv, and sed. + +# check for required utilities +DIRNAME=$(which dirname) || exit 1 +EXIF=$(which exif) || exit 1 +FILE_=$(which file) || exit 1 +GREP=$(which grep) || exit 1 +MV=$(which mv) || exit 1 +SED=$(which sed) || exit 1 +TEST=$(which '[') || exit 1 # but I just use [ in the script while [ -n "$1" ]; do FILE=$1 - TYPE=$(file "$FILE" | grep JPEG) + TYPE=$("${FILE_}" "$FILE" | "${GREP}" JPEG) if [ -n "$TYPE" ]; then FIELD_NAME="$NAME_BY_DATE_FIELD_NAME" if [ -z "$FIELD_NAME" ]; then FIELD_NAME='Date and Time' fi - ASCII_DATE=$(exif -m -t "$FIELD_NAME" "$FILE") # 2010:07:02 08:36:20 + ASCII_DATE=$("${EXIF}" -m -t "$FIELD_NAME" "$FILE") # 2010:07:02 08:36:20 if [ $? -ne 0 ]; then # use similar field, e.g. 'Date and Time (original)' - FIELD=$(exif -m "$FILE" | grep "$FIELD_NAME" | head -n1) - FIELD_NAME=$(echo "$FIELD" | sed 's/\t.*//') + FIELD=$("${EXIF}" -m "$FILE" | "${GREP}" "$FIELD_NAME" | head -n1) + FIELD_NAME=$(echo "$FIELD" | "${SED}" 's/\t.*//') if [ -z "$FIELD_NAME" ]; then echo "$FILE has no stored date" ASCII_DATE="" else echo "$FILE using date from $FIELD_NAME" - ASCII_DATE=$(exif -m -t "$FIELD_NAME" "$FILE") # 2010:07:02 08:36:20 + ASCII_DATE=$("${EXIF}" -m -t "$FIELD_NAME" "$FILE") # 2010:07:02 08:36:20 fi fi - DATE=$(echo "$ASCII_DATE" | sed 's/:/./g' | sed 's/ /./g') + DATE=$(echo "$ASCII_DATE" | "${SED}" 's/:/./g' | "${SED}" 's/ /./g') if [ -z "$DATE" ]; then echo "$FILE has no stored date" else echo "$FILE taken on \"$DATE\"" - FILE_DIR=$(dirname "$FILE") + FILE_DIR=$("${DIRNAME}" "$FILE") NEW_NAME="$FILE_DIR/$DATE.jpg" if [ -e "$NEW_NAME" ] && [ "$NEW_NAME" != "$FILE" ]; then i=1 @@ -61,7 +68,7 @@ while [ -n "$1" ]; do done fi if [ "$NEW_NAME" != "$FILE" ]; then - mv "$FILE" "$NEW_NAME" + "${MV}" "$FILE" "$NEW_NAME" fi fi fi -- 2.26.2