Patch-ID: bash40-021
[bash.git] / examples / scripts.v2 / newext
blob37645bdad93fe1b78a53d089a401b8d3fff712f7
1 #! /bin/bash
3 # original from:
4 # newext: change filename extension
5 # @(#) newext.sh 1.1 93/04/13
6 # 90/06/06 john h. dubois iii (john@armory.com)
7 # 90/11/14 changed ksh-specific code to hybrid: if running under Bourne,
8 # uses expr instead of ksh builtin ops. Removed SYSV specific code.
9 # 91/08/06 added -t option
10 # 92/11/06 made earlier code actually work!
11 # 93/04/13 If no filenames given, act on files in current dir
13 # conversion to bash v2 syntax by Chet Ramey
15 usage="Usage: newext [-th] <oldext> <newext> [filename ...]"
17 phelp()
19 echo "$usage
20 Rename all given files that end in oldext with newext replacing oldext.
21 If no filenames are given, all files in the current directory that end
22 in oldext are acted on (no filename is equivalent to '*').
23 Options:
24 -h: Print this help.
25 -t: Test: No action is taken except to print the mv commands that would
26 be executed if -t was not given."
29 while getopts "th" opt; do
30 case "$opt" in
31 t) echo=echo;;
32 h) phelp; exit 0;;
33 *) echo "$usage" 1>&2; exit 2;;
34 esac
35 done
37 shift $((OPTIND - 1))
39 oldext=$1
40 newext=$2
42 case $# in
43 [01]) echo -e "$usage\nUse -h for help." 1>&2; exit 2;;
44 2) shift ; shift; set -- *;;
45 *) shift ; shift;;
46 esac
48 found=
50 for file
52 case "$file" in
53 *$oldext)
54 newname="${file%$oldext}$newext"
55 $echo mv "$file" "$newname"
56 found=true;;
57 esac
58 done
60 if [ -z "$found" ]; then
61 echo "No files ending in \"$oldext\"."
62 exit 1
64 exit 0