"Fossies" - the Fresh Open Source Software Archive 
Member "tmv-1.1.3/bin/tmv.sh" (5 Feb 2007, 8432 Bytes) of package /linux/privat/old/tmv-1.1.3.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 #!/bin/sh
2 #
3 # copyright 2003-2005 Gene Pavlovsky <heilong@bluebottle.com>
4 #
5 # this is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # tmv: move files/directories according to pathname transformation(s)
11
12 usage()
13 {
14 {
15 echo "Usage: $(basename $0) [options] pathname [pathname]*"
16 echo
17 echo "Moves files/directories according to pathname transformation(s)."
18 echo
19 echo "All non-option arguments are the files/directories to be moved."
20 echo "If none are specified, then the standard input is read."
21 echo
22 echo "Options:"
23 echo -e " --help\t\t\tprint this help, then exit"
24 echo -e " --version\t\t\tprint version number, then exit"
25 echo -e " -e, --expression=SCRIPT\tadd the SCRIPT to sed script"
26 echo -e " -f, --file=FILE\t\tadd the contents of FILE to sed script"
27 echo -e " -r, --regexp-extended\t\tuse extended regexps in sed script"
28 echo -e " -l, --lcase\t\t\tconvert pathnames to lower case"
29 echo -e " -u, --ucase\t\t\tconvert pathnames to upper case"
30 echo -e " --iconv=FROM:TO\t\tconvert pathnames' charset"
31 echo -e " --iconv=FROM\t\tconvert pathnames to locale charset"
32 echo -e " --iconv=:TO\t\tconvert pathnames from locale charset"
33 echo -e " --pipe=COMMAND\t\tpipe pathnames through COMMAND"
34 echo -e " -h, --hand-edit\t\tedit pathnames in an editor"
35 echo -e " -b, --backup\t\t\tbackup files about to be overwritten"
36 echo -e " -q, --quiet\t\t\tdon't explain what's being done"
37 echo -e " -n, --dry-run\t\t\tdon't actually move anything"
38 } >&2
39 quit 2
40 }
41
42 quit()
43 {
44 rm -f "$list_file" "$tmp_file" "$bs_file"
45 test "$1" && exit $1
46 }
47
48 isint()
49 {
50 test $# -eq 0 && return 1
51 while test $# -gt 0; do
52 test "$1" -eq 0 2>/dev/null
53 test $? -eq 2 && return 1
54 shift
55 done
56 return 0
57 }
58
59 strstr()
60 {
61 declare i=0 skip
62 isint "$3" && skip=$3 || skip=0
63 while test $i -le $((${#1}-${#2})); do
64 if test "${1:i:${#2}}" = "$2"; then
65 if test $skip -gt 0; then
66 let --skip
67 let ++i
68 continue
69 fi
70 echo $i
71 return 0
72 fi
73 let ++i
74 done
75 return 1
76 }
77
78 noun_form()
79 {
80 if isint "$2"; then
81 if test $2 -eq 1 -o $2 -eq -1; then
82 echo "$1"
83 else
84 if echo "$1" | grep -e "o$" -e "s$" -e "z$" -e "x$" -e "sh$" -e "ch$" &>/dev/null; then
85 echo "$1es"
86 elif echo "$1" | grep -e "f$" &>/dev/null; then
87 echo "${1:0:${#1}-1}ves"
88 elif echo "$1" | grep -e "fe$" &>/dev/null; then
89 echo "${1:0:${#1}-2}ves"
90 elif echo "$1" | grep "[bcdfghjklmnpqrstvwxz]y$" &>/dev/null; then
91 echo "${1:0:${#1}-1}ies"
92 else
93 echo "$1s"
94 fi
95 fi
96 else
97 echo "$1"
98 fi
99 }
100
101 verb_have_form()
102 {
103 if isint "$1"; then
104 test $1 -eq 1 -o $1 -eq -1 && echo has || echo have
105 else
106 echo has
107 fi
108 }
109
110 verbose=yes
111 file_count=0
112 moved_count=0
113 pipe_count=0
114 list_file=/tmp/tmv.$$
115 tmp_file=/tmp/tmv.tmp.$$
116 bs_file=/tmp/tmv.bs.$$
117 quit
118
119 while test $# -gt 0; do
120 case $1 in
121 --*=*)
122 optarg=$(echo "$1" | sed 's/[-_a-zA-Z0-9]*=//')
123 ;;
124 *)
125 optarg=
126 ;;
127 esac
128
129 case $1 in
130 --help)
131 usage
132 ;;
133 --version)
134 echo "@project@ @version@" >&2
135 quit 2
136 ;;
137 -q|--quiet)
138 verbose=
139 ;;
140 -n|--dry-run)
141 dry_run=yes
142 ;;
143 -e|--expression=*)
144 if test "$1" = "-e"; then
145 shift
146 if test $# -eq 0; then
147 echo "option '$option' requires an argument" >&2
148 quit 1
149 fi
150 optarg="$1"
151 fi
152 sed_expr="$(echo "$optarg" | sed "s/'/'\\\\''/")"
153 test -z "$sed_options" && sed_options="-e '$sed_expr'" || sed_options="$sed_options -e '$sed_expr'"
154 script=yes
155 ;;
156 -f|--file=*)
157 if test "$1" = "-f"; then
158 shift
159 if test $# -eq 0; then
160 echo "option '$option' requires an argument" >&2
161 quit 1
162 fi
163 optarg="$1"
164 fi
165 test -z "$sed_options" && sed_options="-f '$optarg'" || sed_options="$sed_options -f '$optarg'"
166 script=yes
167 ;;
168 -r|--regexp-extended)
169 test -z "$sed_options" && sed_options="-r" || sed_options="$sed_options -r"
170 ;;
171 -l|--lcase)
172 dd_conv=lcase
173 ;;
174 -u|--ucase)
175 dd_conv=ucase
176 ;;
177 --iconv=*)
178 pos=$(strstr "$optarg" :)
179 if test "$pos"; then
180 iconv="-f '${optarg:0:$pos}' -t '${optarg:$((pos+1))}'"
181 else
182 iconv="-f '$optarg'"
183 fi
184 ;;
185 --pipe=*)
186 pipe[$pipe_count]="$optarg"
187 let ++pipe_count
188 ;;
189 -h|--hand-edit|-m|--manual)
190 hand_edit=yes
191 ;;
192 -b|--backup)
193 mv_options='-b'
194 ;;
195 --)
196 end_options=yes
197 ;;
198 -?*)
199 echo "unrecognized option \"$1\"" >&2
200 exit 1
201 ;;
202 *)
203 if test "$1" = '-'; then
204 read_stdin=yes
205 else
206 echo "$1" >>"$list_file"
207 let ++file_count
208 fi
209 ;;
210 esac
211
212 shift
213 test "$end_options" = yes && break
214 done
215
216 if test -z "$script" -a -z "$dd_conv" -a -z "$iconv" -a $pipe_count -eq 0 -a "$hand_edit" != yes; then
217 echo "no sed script(s) or other transformations have been specified" >&2
218 quit 1
219 fi
220
221 if test "$end_options" = yes; then
222 while test $# -gt 0; do
223 echo "$1" >>"$list_file"
224 let ++file_count
225 shift
226 done
227 fi
228 if test $file_count -eq 0 -o "$read_stdin" = yes; then
229 test "$verbose" = yes && echo -n "reading filenames from stdin... " >&2
230 sed 's/\\/\\\\/g' >"$bs_file"
231 while read src; do
232 echo "$src" >>"$list_file"
233 let ++file_count
234 done <"$bs_file"
235 rm -f "$bs_file"
236 test "$verbose" = yes && echo done >&2
237 fi
238
239 if test $file_count -eq 0; then
240 echo "no files have been specified" >&2
241 quit 1
242 fi
243 # strip trailing slashes from pathnames
244 mv "$list_file" "$tmp_file"
245 sed -e 's|/*$||' "$tmp_file" >"$list_file"
246 rm -f "$tmp_file"
247
248 test "$verbose" = yes && echo -n "creating filelist... " >&2
249 i=0
250 sed 's/\\/\\\\/g' "$list_file" >"$bs_file"
251 while read src; do
252 src_files[$i]="$src"
253 let ++i
254 done <"$bs_file"
255 rm -f "$bs_file"
256 test "$verbose" = yes && echo done >&2
257
258 transform()
259 {
260 declare name
261 test $# -gt 1 && name="$2" || name="$1"
262 test "$verbose" = yes && echo -n "transforming: $name " >&2
263 if ! cat "$list_file" | eval "$1" >"$tmp_file"; then
264 test "$verbose" = yes && echo >&2
265 echo "$name has failed, exiting" >&2
266 quit 1
267 fi
268 mv "$tmp_file" "$list_file"
269 test "$verbose" = yes && echo >&2
270 }
271 transform "sed 's/.*\/\([^/]\{1,\}\)\/\?/\1/'" "basename"
272 test "$script" && transform "sed $sed_options"
273 test "$dd_conv" && transform "dd conv=$dd_conv 2>/dev/null" "dd conv=$dd_conv"
274 test "$iconv" && transform "iconv $iconv"
275 if test $pipe_count -gt 0; then
276 for ((i=0; i<pipe_count; ++i)); do
277 transform "${pipe[$i]}"
278 done
279 fi
280 if test "$hand_edit" = yes; then
281 if test "$EDITOR"; then
282 rm -f "$tmp_file"
283 i=0
284 sed 's/\\/\\\\/g' "$list_file" >"$bs_file"
285 while test $i -lt $file_count && read dest; do
286 echo "# ${src_files[$i]}" >>"$tmp_file"
287 echo " $dest" >>"$tmp_file"
288 let ++i
289 done <"$bs_file"
290 rm -f "$bs_file"
291 "$EDITOR" "$tmp_file" && cat "$tmp_file" | grep -v '^# ' | cut -c3- >"$list_file"
292 else
293 echo "set the EDITOR environment variable to your editor of choice first" >&2
294 quit 1
295 fi
296 fi
297
298 test "$verbose" = yes && echo >&2
299 i=0
300 sed 's/\\/\\\\/g' "$list_file" >"$bs_file"
301 while test $i -lt $file_count && read dest; do
302 src="${src_files[$i]}"
303 dest="$(dirname -- "$src")/$dest"
304 test "${src:0:2}" = "./" && src="${src:2}"
305 test "${dest:0:2}" = "./" && dest="${dest:2}"
306 if test "$src" != "$dest" -a "$src" != "$dest/"; then
307 test "$verbose" = yes && echo -e "$(echo $src | sed 's/\\/\\\\/g')\n \033[36m$(echo $dest | sed 's/\\/\\\\/g')\033[0m"
308 if test "$dry_run" != yes; then
309 mv -f $mv_options -- "$src" "$dest" </dev/null || let --moved_count
310 fi
311 let ++moved_count
312 fi
313 let ++i
314 done <"$bs_file"
315 rm -f "$bs_file"
316
317 if test "$verbose" = yes; then
318 test $moved_count -gt 0 && echo >&2
319 test "$dry_run" = yes &&
320 echo -e "\033[37m$moved_count\033[0m/\033[37m$file_count\033[0m $(noun_form file $file_count) would have been moved" >&2 ||
321 echo -e "\033[37m$moved_count\033[0m/\033[37m$file_count\033[0m $(noun_form file $file_count) $(verb_have_form $moved_count) been moved" >&2
322 fi
323
324 quit