home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!ulowell!swlvx2!swlrap.msd.ray.com!mar
- From: mar@swlrap.msd.ray.com (MICHAEL RICHARDS)
- Subject: Re: need script to rename uppercase filenames
- Message-ID: <1992Sep8.131716.5280@swlvx2.msd.ray.com>
- Followup-To: comp.unix.questions
- Keywords: script,ksh
- Sender: news@swlvx2.msd.ray.com (NEWS USER)
- Organization: Raytheon Company, Tewksbury, Mass.
- References: <699@svcs1.UUCP> <Bu74sM.Hxz@multisys.com>
- Date: Tue, 8 Sep 1992 13:17:16 GMT
- Lines: 122
-
- Here is a korn shell script to convert a list of file names to
- upper or lower case. It is probibly better to rename the files
- and then convert the files from msdos format.
-
- Michael Richards (mar@swl.msd.ray.com)
-
- #!/usr/bin/ksh
- #-----------------------------------------------------------------------#
- # FUNCTIONAL DESCRIPTION: #
- # This shell script will rename a file changing the case of the #
- # filename to case on the command line (upper case or lower) The #
- # script will only change the case of the file name not a full #
- # path. #
- # #
- # Usage: convert_case -[ul] [-n] [-v] file [...] #
- # u upper case #
- # l upper case #
- # n no execute #
- # v verbose #
- #-----------------------------------------------------------------------#
-
- usage ()
- {
- print -u2 "Usage: ${1##*/} -[ul] [-n] [-v] file [...]"
- exit $2
- }
-
- if (( $# < 2 ))
- then
- usage $0 1
- fi
- integer orig=$# exit_status=0
- typeset file no_execute= verbose= name_only=
-
- while getopts ulvnp c
- do
- case $c in
-
- l)
- typeset -l new_file_base
- (( exit_status += 1 ))
- ;;
- u)
- typeset -u new_file_base
- (( exit_status += 1 ))
- ;;
- n)
- no_execute=":"
- ;;
- v)
- verbose="on"
- ;;
- p)
- name_only="on"
- ;;
- *)
- print -u2 "$0: unknown option"
- usage $0 2
- ;;
- esac
- done
- shift OPTIND-1
-
- if (( $# == orig || exit_status == 0 ))
- then
- usage $0 3
- fi
- (( exit_status = 0 ))
-
- while (( $# > 0 ))
- do
-
- # get the filename #
- file=$1
- shift
-
- new_file_base=${file##*/}
-
- #if the filename is a path get the path part #
- file_path=${file%/*}
-
- case $file in
- */* )
- file_path=$file_path/
- ;;
- *)
- file_path=
- ;;
- esac
-
- if [[ $file = $file_path$new_file_base ]]
- then
- print -u2 "${0##*/}: '$new_file_base' is already lower case"
- continue
- fi
-
- if [[ -n ${verbose} ]]
- then
- print -u2 -- "mv $file $file_path$new_file_base"
- fi
- if [[ -n ${name_only} ]]
- then
- print -- $file_path$new_file_base
- fi
-
- ${no_execute} mv $file $file_path$new_file_base
-
- exit_status=$?
- if [[ $exit_status != 0 ]]
- then
- print -u2 "${0##*/}:mv failed on '$file' with exit status $exit_status"
- if (( $# == 1 ))
- then
- print -u2 "1 file left: $@ "
- elif (( $# != 0 ))
- then
- print -u2 "$# files left: $@ "
- fi
- exit exit_status
- fi
- done
- exit 0
-