From 772f20abb0a3a0979c440114bf3a1cff5b3cef03 Mon Sep 17 00:00:00 2001 From: cvpcs Date: Wed, 2 Jun 2010 11:02:31 -0500 Subject: initial import of bash 4.1 --- examples/scripts.v2/rename | 122 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 examples/scripts.v2/rename (limited to 'examples/scripts.v2/rename') diff --git a/examples/scripts.v2/rename b/examples/scripts.v2/rename new file mode 100644 index 0000000..96c46d6 --- /dev/null +++ b/examples/scripts.v2/rename @@ -0,0 +1,122 @@ +#! /bin/bash +# +# original from: +# @(#) rename.ksh 1.1 94/05/10 +# 90/06/01 John DuBois (spcecdt@armory.com) +# 91/02/25 Improved help info +# 92/06/07 remove quotes from around shell pattern as required by new ksh +# 94/05/10 Exit if no globbing chars given. +# +# conversion to bash v2 syntax by Chet Ramey + +phelp() +{ +echo "$usage +All files that match oldpattern will be renamed with the +filename components that match the constant parts of oldpattern +changed to the corresponding constant parts of newpattern. +The components of the filename that match variable parts of +oldpattern will be preserved. Variable parts in oldpattern +must occur in the same order in newpattern. Variables parts +can be '?' and '*'. +Example: +rename \"/tmp/foo*.ba.?\" \"/tmp/new*x?\" +All files in /tmp that match foo*.ba.? will have the \"foo\" part +replaced by \"new\" and the \".ba.\" part replaced by \"x\"." +} + +usage="usage: $name [-htv] oldpattern newpattern" +name=${0##/} + +while getopts "htv" opt; do + case "$opt" in + t) tell=true;; + v) verbose=true;; + h) phelp; exit 0;; + *) echo "$name: $usage" 1>&2; exit 2;; + esac +done +shift $((OPTIND - 1)) + +if [ $# -lt 2 ]; then + phelp + exit 2 +fi + +oldpat=$1 +newpat=$2 + +set -- $1 +if [ ! -e "$1" ]; then + echo "$name: no files match $oldpat." + exit 1 +fi + +typeset -i i=1 j + +# Example oldpat: foo*.a +# Example newpat: bar*.b + +# Examples given for first iteration (in the example, the only interation) +while :; do + case "$oldpat" in + *[\*\?]*) ;; + *) break;; + esac + + # Get leftmost globbing pattern in oldpat + pat=${oldpat#*[\*\?]} # pat=.a + pat=${oldpat%%"$pat"} # pat=foo* + pat=${pat##*[!\?\*]} # pat=* + # Find parts before & after pattern + oldpre[i]=${oldpat%%"$pat"*} # oldpre[1]=foo + oldsuf[i]=${oldpat#*"$pat"} # oldsuf[1]=.a + newpre[i]=${newpat%%"$pat"*} # newpre[1]=bar + # Get rid of processed part of patterns + oldpat=${oldpat#${oldpre[i]}"$pat"} # oldpat=.a + newpat=${newpat#${newpre[i]}"$pat"} # newpat=.b + let i=i+1 +done + +if [ $i -eq 1 ]; then + echo "No globbing chars in pattern." 1>&2 + exit 1 +fi + +oldpre[i]=${oldpat%%"$pat"*} # oldpre[2]=.a +oldsuf[i]=${oldpat#*"$pat"} # oldsuf[2]=.a +newpre[i]=${newpat%%"$pat"*} # newpre[2]=.b + +if [ -n "$verbose" ]; then + j=1 + while let "j < i"; do + echo \ +"Old prefix: ${oldpre[j]} Old suffix: ${oldsuf[j]} New prefix: ${newpre[j]}" + let j=j+1 + done +fi + +# Example file: foox.a + +for file; do + j=1 + origname=$file # origname=foox.a + newfile= + while let "j <= i"; do + # Peel off a prefix interation 1 2 + file=${file#${oldpre[j]}} # file=x.a file= + # Save the part of this prefix that is to be retained + const=${file%${oldsuf[j]}} # const=x const= + newfile=$newfile${newpre[j]}$const # newfile=barx newfile=barx.b + file=${file#$const} # file=.a file=.a + let j=j+1 + done + if [ -n "$tell" ]; then + echo "Would move \"$origname\" to \"$newfile\"." + else + if [ -n "$verbose" ]; then + echo "Moving \"$origname\" to \"$newfile\"." + fi + mv $origname $newfile + fi +done -- cgit v1.1