jpgHasDate 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #! /bin/bash
  2. #title :jpgHasDate.sh
  3. #description :Determine if an JPEG file has a date
  4. #author :gmj@pobox.com
  5. #date :Sat Dec 19 15:35:06 2015
  6. #version :0.1
  7. #usage :jpgHasDate.sh [optinos] FILE [FILE...]
  8. #notes :Experimeting with well-writtne bash
  9. #bash_version :
  10. #==============================================================================
  11. set -u # exit on undefined
  12. set -e # exit on error
  13. # set -x # echo during execution
  14. # Useful tips
  15. # http://www.kfirlavi.com/blog/2012/11/14/defensive-bash-programming/
  16. readonly PROGNAME=$(basename $0)
  17. readonly PROGDIR=$(readlink -m $(dirname $0))
  18. readonly ARGS="$@"
  19. usage() {
  20. if [ $# -gt 0 ]; then echo "${PROGNAME}: $@"; echo; fi
  21. cat <<- EOF
  22. usage: $PROGNAME [options] FILE [FILE...]
  23. This program determins if a JPEG file has an Original Date stored
  24. with it and prints the filename and/or date.
  25. OPTIONS:
  26. -w --withdates Select files with dates
  27. -m --missingdates Select files with no dates
  28. -p --path Print path of matching files
  29. -t --dates Print dates of matching files
  30. -v --verbose Verbose. You can specify more then one -v to have more verbose
  31. -d --debug debug
  32. -h --help show this help
  33. Examples:
  34. print info about a single file
  35. $PROGNAME --test all
  36. EOF
  37. }
  38. parseArgs() {
  39. # Option parsing courtesy of
  40. # http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options
  41. # NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
  42. # separately; see below.
  43. TEMP=`getopt -o wmptvdh --long withdates,missingdates,path,dates,verbose,debug,help \
  44. -n 'jpghasDate' -- "$@"`
  45. if [ $? != 0 ] ; then echo "getopt failed. Terminating..." >&2 ; exit 1 ; fi
  46. # Note the quotes around `$TEMP': they are essential!
  47. eval set -- "$TEMP"
  48. # Set default values
  49. # Selection Parameters
  50. OPT_SELECT_WITH_DATES=false # print files that have dates
  51. OPT_SELECT_MISSING_DATES=false # print files with no date
  52. # Output Parameters
  53. OPT_VERBOSE=false # verbose output
  54. OPT_DEBUG=false # debugging output
  55. OPT_PRINT_PATH=false # print path
  56. OPT_PRINT_DATES=false # print date
  57. OPT_PRINT_ALL=true # print all (default)
  58. while true; do
  59. case "$1" in
  60. -w | --withdates ) OPT_SELECT_WITH_DATES=true; shift ;;
  61. -m | --missingdates ) OPT_SELECT_MISSING_DATES=true; shift ;;
  62. -p | --path ) OPT_PRINT_PATH=true; shift ;;
  63. -t | --dates ) OPT_PRINT_DATES=true; shift ;;
  64. -v | --verbose ) OPT_VERBOSE=true; shift ;;
  65. -d | --debug ) OPT_DEBUG=true; shift ;;
  66. -h | --help ) usage; exit 0;shift ;;
  67. -- ) shift; break ;;
  68. * ) break ;;
  69. esac
  70. done
  71. if [ $# -lt 1 ] ; then usage "Missing argument. Need at least one filename."; exit 1; fi
  72. # Defualt to printing all
  73. if ! ( $OPT_PRINT_PATH || $OPT_PRINT_DATES ); then
  74. OPT_PRINT_PATH=true
  75. OPT_PRINT_DATES=true
  76. fi
  77. # Defualt to selectiing all
  78. if ! ( $OPT_SELECT_WITH_DATES || $OPT_SELECT_MISSING_DATES ); then
  79. OPT_SELECT_WITH_DATES=true
  80. OPT_SELECT_MISSING_DATES=true
  81. fi
  82. # Make parsing paramters readonly
  83. readonly OPT_SELECT_WITH_DATES=${OPT_SELECT_WITH_DATES} # print files that have dates
  84. readonly OPT_SELECT_MISSING_DATES=${OPT_SELECT_MISSING_DATES} # print files with no date
  85. readonly OPT_VERBOSE=${OPT_VERBOSE} # verbose output
  86. readonly OPT_DEBUG=${OPT_DEBUG} # debugging output
  87. readonly OPT_PRINT_PATH=${OPT_PRINT_PATH} # print path
  88. readonly OPT_PRINT_DATES=${OPT_PRINT_DATES} # print date
  89. readonly OPT_PRINT_ALL=${OPT_PRINT_ALL} # print all (default)
  90. readonly OPT_ARGS="$@"
  91. }
  92. printSelectedFiles () {
  93. for file in "$*"; do
  94. if $OPT_DEBUG; then echo file $file >&2; fi
  95. date=$(exiftool $file | grep Orig || echo "NONE")
  96. date=$(echo $date | sed -e 's/Date\/Time Original\s*\:\s*//')
  97. output=""
  98. if $OPT_PRINT_PATH; then output="$file"; fi
  99. if $OPT_PRINT_DATES; then output="$output : $date"; fi
  100. if $OPT_SELECT_MISSING_DATES; then
  101. if [[ $date == *"NONE"* ]]; then
  102. echo $output
  103. fi
  104. fi
  105. if $OPT_SELECT_WITH_DATES; then
  106. if [[ $date != *"NONE"* ]]; then
  107. echo $output
  108. fi
  109. fi
  110. done
  111. }
  112. main() {
  113. parseArgs $ARGS
  114. printSelectedFiles $OPT_ARGS
  115. }
  116. main