| Line | |
|---|
| 1 | #! /bin/bash
|
|---|
| 2 | #
|
|---|
| 3 | # original from:
|
|---|
| 4 | # newext: change filename extension
|
|---|
| 5 | # @(#) newext.sh 1.1 93/04/13
|
|---|
| 6 | # 90/06/06 john h. dubois iii ([email protected])
|
|---|
| 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
|
|---|
| 12 | #
|
|---|
| 13 | # conversion to bash v2 syntax by Chet Ramey
|
|---|
| 14 |
|
|---|
| 15 | usage="Usage: newext [-th] <oldext> <newext> [filename ...]"
|
|---|
| 16 |
|
|---|
| 17 | phelp()
|
|---|
| 18 | {
|
|---|
| 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."
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 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
|
|---|
| 36 |
|
|---|
| 37 | shift $((OPTIND - 1))
|
|---|
| 38 |
|
|---|
| 39 | oldext=$1
|
|---|
| 40 | newext=$2
|
|---|
| 41 |
|
|---|
| 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
|
|---|
| 47 |
|
|---|
| 48 | found=
|
|---|
| 49 |
|
|---|
| 50 | for file
|
|---|
| 51 | do
|
|---|
| 52 | case "$file" in
|
|---|
| 53 | *$oldext)
|
|---|
| 54 | newname="${file%$oldext}$newext"
|
|---|
| 55 | $echo mv "$file" "$newname"
|
|---|
| 56 | found=true;;
|
|---|
| 57 | esac
|
|---|
| 58 | done
|
|---|
| 59 |
|
|---|
| 60 | if [ -z "$found" ]; then
|
|---|
| 61 | echo "No files ending in \"$oldext\"."
|
|---|
| 62 | exit 1
|
|---|
| 63 | fi
|
|---|
| 64 | exit 0
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.