| 1 |
#!/bin/sh |
| 2 |
# @(#) $Id: exifgrep,v 1.11 2005/07/02 13:10:03 alex Exp $ |
| 3 |
|
| 4 |
# Run 'egrep' on the output of exifprobe -L, massaging the output a la |
| 5 |
# 'list-photo'. Accepts the -var, -export, -num, and '-' options of |
| 6 |
# list_photo; all other options are passed to egrep. |
| 7 |
|
| 8 |
usage() |
| 9 |
{ |
| 10 |
echo "Usage: exifgrep [-var|-export] [-num] [-r] [-t] [-n] [-c] [egrep-options] egrep-pattern [NOT egrep-pattern] imagefilename[s]" 1>&2 |
| 11 |
exit 1 |
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
# Create the output format, choosing numerical or "interpreted" values |
| 16 |
# from the exifprobe output lines, and stripping unwanted spaces for |
| 17 |
# "variable" format (while quoting multi-word values). |
| 18 |
|
| 19 |
format() |
| 20 |
{ |
| 21 |
filename= |
| 22 |
vars="var nequals nvalue iequals value remainder" |
| 23 |
case "${nameopt}" in |
| 24 |
"-n") vars="filename var nequals nvalue iequals value remainder" ;; |
| 25 |
esac |
| 26 |
|
| 27 |
while read $vars |
| 28 |
do |
| 29 |
case "$filename" in |
| 30 |
"#") continue ;; |
| 31 |
"#"*) continue; ;; |
| 32 |
*"#") continue ;; |
| 33 |
esac |
| 34 |
case ${var} in |
| 35 |
"#") continue ;; |
| 36 |
"#"*) continue; ;; |
| 37 |
*"#") continue ;; |
| 38 |
esac |
| 39 |
if test 0$dovar -ne 0 |
| 40 |
then |
| 41 |
if test ${donum} -eq 1 |
| 42 |
then |
| 43 |
case ${nvalue} in |
| 44 |
[0-9]*) echo -n "${export}${var}=${nvalue}" ;; |
| 45 |
*) quote ${var} ${nvalue} ${iequals} ${value} ${remainder};; |
| 46 |
esac |
| 47 |
else |
| 48 |
case "${iequals}" in |
| 49 |
"") quote ${var} ${nvalue}${value}${remainder} ;; |
| 50 |
=) quote ${var} ${value} ${remainder} ;; |
| 51 |
APEX) quote ${var} ${remainder} ;; |
| 52 |
*) quote ${var} ${nvalue} ${iequals} ${value} ${remainder} ;; |
| 53 |
esac |
| 54 |
fi |
| 55 |
elif test ${donum} -eq 1 |
| 56 |
then |
| 57 |
case ${nvalue} in |
| 58 |
"'"*) echo -n "${var}=${nvalue}" |
| 59 |
case "${iequals}" in |
| 60 |
"") ;; |
| 61 |
*) echo -n " ${iequals} ${value} ${remainder}" ;; |
| 62 |
esac |
| 63 |
;; |
| 64 |
*) case "${var}" in |
| 65 |
"#") ;; |
| 66 |
*) echo -n "${var}=${nvalue}" ;; |
| 67 |
esac |
| 68 |
;; |
| 69 |
esac |
| 70 |
else |
| 71 |
echo -n $var $nequals $nvalue $iequals $value $remainder |
| 72 |
fi |
| 73 |
case "${filename}" in |
| 74 |
"") echo ;; |
| 75 |
*) echo " # ${filename}" ;; |
| 76 |
esac |
| 77 |
done |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
# On output, place double-quotes around multi-word values which |
| 82 |
# are not already quoted. This is necessary for 'variable' format. |
| 83 |
# Do not include "commented" material in quoted phrase. |
| 84 |
|
| 85 |
quote() |
| 86 |
{ |
| 87 |
local inquote |
| 88 |
|
| 89 |
inquote=0 |
| 90 |
case $# in |
| 91 |
0) ;; |
| 92 |
1) echo -n "${export}$1=" ;; |
| 93 |
2) echo -n "${export}$1=$2" ;; |
| 94 |
*) case $1 in |
| 95 |
\#*) echo -n "$1" ;; |
| 96 |
*) echo -n "${export}$1"= ;; |
| 97 |
esac |
| 98 |
shift |
| 99 |
while test $# -gt 0 |
| 100 |
do |
| 101 |
case $1 in |
| 102 |
\'*) echo -n $*; break ;; |
| 103 |
\#*) case $inquote in |
| 104 |
1) echo -n "' " ;; |
| 105 |
esac |
| 106 |
inquote=0 |
| 107 |
echo -n "$*" |
| 108 |
break |
| 109 |
;; |
| 110 |
*) case $inquote in |
| 111 |
0) echo -n "'"; inquote=1 ;; |
| 112 |
esac |
| 113 |
echo -n "$1" |
| 114 |
shift |
| 115 |
;; |
| 116 |
esac |
| 117 |
done |
| 118 |
case $inquote in |
| 119 |
1) echo -n "'" ;; |
| 120 |
esac |
| 121 |
;; |
| 122 |
esac |
| 123 |
} |
| 124 |
|
| 125 |
exifgrep_imagefind() |
| 126 |
{ |
| 127 |
local dir |
| 128 |
|
| 129 |
dir="$*" |
| 130 |
find ${dir:="."} \ |
| 131 |
\( \ |
| 132 |
-iname "*.tiff" -o -iname "*.tiff" -o -iname "*.tiff.Z" \ |
| 133 |
-o -iname "*.tif" -o -iname "*.tif" -o -iname "*.tif.Z" \ |
| 134 |
-o -iname "*.jpeg" -o -iname "*.jpeg.gz" -o -iname "*.jpeg.Z" \ |
| 135 |
-o -iname "*.jpg" -o -iname "*.jpg.gz" -o -iname "*.jpg.Z" \ |
| 136 |
-o -iname "*.jpe" -o -iname "*.jpe.gz" -o -iname "*.jpe.Z" \ |
| 137 |
-o -iname "*.kqp" -o -iname "*.kqp.gz" -o -iname "*.kqp.Z" \ |
| 138 |
-o -iname "*.mrw" -o -iname "*.mrw.gz" -o -iname "*.mrw.Z" \ |
| 139 |
-o -iname "*.nef" -o -iname "*.nef.gz" -o -iname "*.nef.Z" \ |
| 140 |
-o -iname "*.orf" -o -iname "*.orf.gz" -o -iname "*.orf.Z" \ |
| 141 |
-o -iname "*.pef" -o -iname "*.pef.gz" -o -iname "*.pef.Z" \ |
| 142 |
-o -iname "*.x3f" -o -iname "*.x3f.gz" -o -iname "*.x3f.Z" \ |
| 143 |
-o -iname "*.dng" -o -iname "*.dng.gz" -o -iname "*.dng.Z" \ |
| 144 |
-o -iname "*.erf" -o -iname "*.erf.gz" -o -iname "*.erf.Z" \ |
| 145 |
-o -iname "*.raf" -o -iname "*.raf.gz" -o -iname "*.raf.Z" \ |
| 146 |
-o -iname "*.cr2" -o -iname "*.cr2.gz" -o -iname "*.cr2.Z" \ |
| 147 |
-o -iname "*.crw" -o -iname "*.crw.gz" -o -iname "*.crw.Z" \ |
| 148 |
-o -iname "*.jp2" -o -iname "*.jp2.gz" -o -iname "*.jp2.Z" \ |
| 149 |
-o -iname "*.k25" -o -iname "*.k25.gz" -o -iname "*.k25.Z" \ |
| 150 |
-o -iname "*.kdc" -o -iname "*.kdc.gz" -o -iname "*.kdc.Z" \ |
| 151 |
\) -print |
| 152 |
} |
| 153 |
|
| 154 |
: PROBE=${PROBE:=exifprobe} |
| 155 |
# Gather options first. This is kind of wierd. |
| 156 |
dovar=0 |
| 157 |
donum=0 |
| 158 |
recursisve=0 |
| 159 |
|
| 160 |
export= |
| 161 |
egrep_options= |
| 162 |
vgrep_options= |
| 163 |
pattern= |
| 164 |
notpattern= |
| 165 |
nameopt= |
| 166 |
tagopt= |
| 167 |
coloropt= |
| 168 |
|
| 169 |
while test $# -ge 1 |
| 170 |
do |
| 171 |
case $1 in |
| 172 |
-var) dovar=1; shift ;; |
| 173 |
-num) donum=1; shift ;; |
| 174 |
-c) coloropt="-c" ; shift ;; |
| 175 |
-n) nameopt="-n"; shift ;; # may need this if reading from stdin |
| 176 |
-r) recursive=1; shift ;; |
| 177 |
-t) tagopt="-enN" ; shift ;; |
| 178 |
-export*) export="export "; dovar=1; shift ;; |
| 179 |
-h) usage ;; |
| 180 |
-*) egrep_options="$egrep_options $1"; shift ;; |
| 181 |
NOT) case "$2" in |
| 182 |
-f) notpattern="-f $3"; shift; shift; shift; break ;; |
| 183 |
*) notpattern="$2"; shift; shift; break ;; |
| 184 |
esac |
| 185 |
;; |
| 186 |
*) pattern="$1"; shift |
| 187 |
case $1 in |
| 188 |
NOT) case "$2" in |
| 189 |
-f) notpattern="-f $3"; shift; shift; shift; break ;; |
| 190 |
*) notpattern="$2"; shift; shift; break ;; |
| 191 |
esac |
| 192 |
;; |
| 193 |
*) break ;; |
| 194 |
esac |
| 195 |
;; |
| 196 |
esac |
| 197 |
done |
| 198 |
|
| 199 |
if test $# -lt 1 -a "X${pattern}${notpattern}" = "X" |
| 200 |
then |
| 201 |
usage |
| 202 |
fi |
| 203 |
|
| 204 |
# This should probably be modified somehow to use xargs. |
| 205 |
case "${recursive}" in |
| 206 |
1) args=$( exifgrep_imagefind $* ) |
| 207 |
set -- ${args} |
| 208 |
;; |
| 209 |
esac |
| 210 |
|
| 211 |
# All variables are global. |
| 212 |
# If there are no filenames, read from stdin, which is expected to be |
| 213 |
# exifprobe -L output. |
| 214 |
|
| 215 |
case $# in |
| 216 |
0) egrep ${egrep_options} "${pattern}" | format |
| 217 |
;; |
| 218 |
*) case $# in |
| 219 |
1) ;; |
| 220 |
*) nameopt="-n" ;; # Use names if more than one file |
| 221 |
esac |
| 222 |
case "${notpattern}" in |
| 223 |
"") ${PROBE} -L ${coloropt} ${nameopt} ${tagopt} $* | egrep ${egrep_options} "${pattern}" | format ;; |
| 224 |
*) case "${pattern}" in |
| 225 |
"") ${PROBE} -L ${coloropt} ${nameopt} ${tagopt} $* | egrep ${egrep_options} -v "${notpattern}" | format ;; |
| 226 |
*) ${PROBE} -L ${coloropt} ${nameopt} ${tagopt} $* | egrep ${egrep_options} "${pattern}" | egrep -v "${notpattern}" | format ;; |
| 227 |
esac |
| 228 |
;; |
| 229 |
esac |
| 230 |
;; |
| 231 |
esac |