| 1 | #! /bin/bash
|
|---|
| 2 | #
|
|---|
| 3 | # original from:
|
|---|
| 4 | # @(#) rename.ksh 1.1 94/05/10
|
|---|
| 5 | # 90/06/01 John DuBois ([email protected])
|
|---|
| 6 | # 91/02/25 Improved help info
|
|---|
| 7 | # 92/06/07 remove quotes from around shell pattern as required by new ksh
|
|---|
| 8 | # 94/05/10 Exit if no globbing chars given.
|
|---|
| 9 | #
|
|---|
| 10 | # conversion to bash v2 syntax by Chet Ramey
|
|---|
| 11 |
|
|---|
| 12 | phelp()
|
|---|
| 13 | {
|
|---|
| 14 | echo "$usage
|
|---|
| 15 | All files that match oldpattern will be renamed with the
|
|---|
| 16 | filename components that match the constant parts of oldpattern
|
|---|
| 17 | changed to the corresponding constant parts of newpattern.
|
|---|
| 18 | The components of the filename that match variable parts of
|
|---|
| 19 | oldpattern will be preserved. Variable parts in oldpattern
|
|---|
| 20 | must occur in the same order in newpattern. Variables parts
|
|---|
| 21 | can be '?' and '*'.
|
|---|
| 22 | Example:
|
|---|
| 23 | rename \"/tmp/foo*.ba.?\" \"/tmp/new*x?\"
|
|---|
| 24 | All files in /tmp that match foo*.ba.? will have the \"foo\" part
|
|---|
| 25 | replaced by \"new\" and the \".ba.\" part replaced by \"x\"."
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | usage="usage: $name [-htv] oldpattern newpattern"
|
|---|
| 29 | name=${0##/}
|
|---|
| 30 |
|
|---|
| 31 | while getopts "htv" opt; do
|
|---|
| 32 | case "$opt" in
|
|---|
| 33 | t) tell=true;;
|
|---|
| 34 | v) verbose=true;;
|
|---|
| 35 | h) phelp; exit 0;;
|
|---|
| 36 | *) echo "$name: $usage" 1>&2; exit 2;;
|
|---|
| 37 | esac
|
|---|
| 38 | done
|
|---|
| 39 | shift $((OPTIND - 1))
|
|---|
| 40 |
|
|---|
| 41 | if [ $# -lt 2 ]; then
|
|---|
| 42 | phelp
|
|---|
| 43 | exit 2
|
|---|
| 44 | fi
|
|---|
| 45 |
|
|---|
| 46 | oldpat=$1
|
|---|
| 47 | newpat=$2
|
|---|
| 48 |
|
|---|
| 49 | set -- $1
|
|---|
| 50 | if [ ! -e "$1" ]; then
|
|---|
| 51 | echo "$name: no files match $oldpat."
|
|---|
| 52 | exit 1
|
|---|
| 53 | fi
|
|---|
| 54 |
|
|---|
| 55 | typeset -i i=1 j
|
|---|
| 56 |
|
|---|
| 57 | # Example oldpat: foo*.a
|
|---|
| 58 | # Example newpat: bar*.b
|
|---|
| 59 |
|
|---|
| 60 | # Examples given for first iteration (in the example, the only interation)
|
|---|
| 61 | while :; do
|
|---|
| 62 | case "$oldpat" in
|
|---|
| 63 | *[\*\?]*) ;;
|
|---|
| 64 | *) break;;
|
|---|
| 65 | esac
|
|---|
| 66 |
|
|---|
| 67 | # Get leftmost globbing pattern in oldpat
|
|---|
|
|---|