brian@hpausla.UUCP (03/12/87)
This is a quickie shell script that I think is rather fun. It allows you to change the names of a set of files by deleting or adding character patterns. The '=' character acts as a wildcard much like the '*' does in many PIP variants, ie: it can be used in the target file name and will expand to whatever it matched in the source file name. '=' was chosen as it is not expanded by the shell; '\*' should also work, but doesn't. For instance, to remove .old extensions from a set of C source files, you could use: mved =.c.old old/=.c To add a 'lib' prefix to all archives in the current directory use: mved =.a lib=.a You can use he '-n' option to see what mved would do if you're unsure. Use it if you're at all unsure of the results, especially for the first few runs. The script has only been used on a Sys V system. It's yours to use, at your own risk. Brian Coogan, ex Hewlett-Packard Australian Software Operation. ps: Please don't mail to this account as it's unlikely to get read! #---------------------------------- cut here ---------------------------------- # This is a shell archive. Remove anything before this line, # then unpack it by saving it in a file and typing "sh file". # # Wrapped by brian at hpausla on Thu Mar 12 18:13:05 1987 # # This archive contains: # mved.sh # # Error checking via wc(1) will be performed. echo x - mved.sh cat >mved.sh <<'@EOF' #! /bin/sh # mved.sh # Move-and-edit filenames. # # Usage: mved [-n] from-pattern to-pattern # # This command allows you to change file names much as is possible # with some versions of PIP (remember *.txt=*.bak?). # The '=' character in from-pattern is treated as a special wildcard, # matching in the same way as the shell '*' wildcard character, except # that the text matching the '=' in the first pattern is inserted in # place of any = wildcards in the second. # Note that from-pattern need not have a wildcard if to-pattern does, # a default # # Use the '-n' option to do nothing, showing what would be done. # # Restrictions: # Only the first '=' sign in from-pattern is used. Multiple = # wildcards in from-pattern match up with the first from-pattern # =, ie: there is no matching for multiple = signs. (I'm sure # someone could make it work if they wanted to... ?) # # eg: mved lib=.a =.a moves libhello.a to hello.a # mved =.o =.o.old moves fred.o to fred.o.old # mved '=.*' = moves fred.junk to fred # mved =.sh = moves mved.sh to mved # mved *.sh =. # # Brian Coogan 06 Jan 87 # Hewlett-Packard Australian Software Operation # $Header$ ASO shopt=x case "$1" in -n) shopt=vn; shift ;; esac # Check for appropriate wildcards. # Source must have an = or a * wildcard or already exist. case "$1" in *=*) ;; *) for n in $1 do if [ ! -f "$n" ] then echo "$0: No files match from-pattern!\n" 1>&2 set -- "$@" give usage message elif [ "$2" = '=' ] then echo Nothing doing. exit 0 fi break done ;; esac case "$2" in *=*) ;; *) echo "$0: No '=' wildcards used in target!\n" 1>&2 set -- "$@" give usage message ;; esac # catch mved = = case "$1$2" in ==) echo Nothing doing.; exit 0;; esac if [ $# -ne 2 ] then echo "Usage: $0 [-n] from-pattern to-pattern" 1>&2 echo "\tEquals (=) signs in the to-pattern match like '*' and are" echo "\treplaced with the text that matched the = in from-pattern." echo "\tYou must quote any '*'s in from-pattern." exit 1 fi globpatt=`echo $1 | sed 's/=/\*/'` frompatt=`echo "$1" | sed \ -e 's/\./\\\\./g' \ -e 's/\*/.*/g' \ -e 's/=/\\\\(\\.\\*\\\\)/' \ -e '/\\\\(/ !s/.*/\\\\(&\\\\)/' ` topatt=`echo "$2" | sed -e 's/=/\\\\1/g'` for n in $globpatt do # Check the pattern got expanded. (The file might also have vanished). if [ ! -f $n ] then echo "$0: No files matching $1 found." 1>&2 exit 1 fi echo $n done | sed -n "s;$frompatt;mv & $topatt;p" | sh -$shopt echo done @EOF if test "`wc -lwc <mved.sh`" != ' 102 454 2480' then echo ERROR: wc results of mved.sh are `wc -lwc <mved.sh` should be 102 454 2480 fi chmod 775 mved.sh exit 0