home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / question / 10821 < prev    next >
Encoding:
Text File  |  1992-09-08  |  2.8 KB  |  136 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!ulowell!swlvx2!swlrap.msd.ray.com!mar
  3. From: mar@swlrap.msd.ray.com (MICHAEL RICHARDS)
  4. Subject: Re: need script to rename uppercase filenames
  5. Message-ID: <1992Sep8.131716.5280@swlvx2.msd.ray.com>
  6. Followup-To: comp.unix.questions
  7. Keywords: script,ksh
  8. Sender: news@swlvx2.msd.ray.com (NEWS USER)
  9. Organization: Raytheon Company, Tewksbury, Mass.
  10. References: <699@svcs1.UUCP> <Bu74sM.Hxz@multisys.com>
  11. Date: Tue, 8 Sep 1992 13:17:16 GMT
  12. Lines: 122
  13.  
  14. Here is a korn shell script to convert a list of file names to
  15. upper or lower case. It is probibly better to rename the files
  16. and then convert the files from msdos format. 
  17.  
  18. Michael Richards (mar@swl.msd.ray.com)
  19.  
  20. #!/usr/bin/ksh
  21. #-----------------------------------------------------------------------#
  22. #    FUNCTIONAL DESCRIPTION:                        #
  23. #    This shell script will rename a file changing the case of the    #
  24. #    filename to case on the command line (upper case or lower) The    #
  25. #    script will only change the case of the file name not a full    #
  26. #    path.                                #
  27. #                                    #
  28. #   Usage: convert_case -[ul] [-n] [-v] file [...]            #
  29. #      u upper case                            #
  30. #      l upper case                            #
  31. #      n no execute                            #
  32. #      v verbose                            #
  33. #-----------------------------------------------------------------------#
  34.  
  35. usage ()
  36.     {
  37.     print -u2 "Usage: ${1##*/} -[ul] [-n] [-v] file [...]"
  38.     exit $2
  39.     }
  40.  
  41. if (( $# < 2 ))
  42.     then
  43.     usage $0 1
  44.     fi
  45. integer  orig=$# exit_status=0 
  46. typeset file no_execute= verbose= name_only=
  47.  
  48. while getopts ulvnp  c
  49.    do
  50.    case $c in
  51.  
  52.    l)
  53.     typeset -l new_file_base
  54.     (( exit_status += 1 ))
  55.     ;;
  56.    u)
  57.     typeset -u new_file_base
  58.     (( exit_status += 1 ))
  59.     ;;
  60.    n)
  61.     no_execute=":"
  62.     ;;
  63.    v)
  64.     verbose="on"
  65.     ;;
  66.    p)
  67.     name_only="on"
  68.     ;;
  69.    *)
  70.     print -u2 "$0: unknown option"
  71.     usage $0 2
  72.     ;;
  73.    esac
  74.    done
  75. shift OPTIND-1
  76.  
  77. if (( $# == orig || exit_status == 0  ))
  78.    then
  79.    usage $0 3
  80.    fi
  81. (( exit_status = 0 ))
  82.  
  83. while (( $# > 0 ))
  84.     do
  85.  
  86.     # get the filename                            #
  87.     file=$1
  88.     shift
  89.  
  90.     new_file_base=${file##*/}
  91.  
  92.     #if the filename is a path get the path part            #
  93.     file_path=${file%/*}
  94.  
  95.     case $file in
  96.        */* )
  97.       file_path=$file_path/
  98.       ;;
  99.        *)
  100.       file_path=
  101.       ;;
  102.     esac
  103.  
  104.     if [[ $file = $file_path$new_file_base ]]
  105.     then
  106.     print -u2 "${0##*/}: '$new_file_base' is already lower case"
  107.     continue
  108.     fi
  109.  
  110.     if [[ -n ${verbose} ]]
  111.     then
  112.         print -u2 -- "mv $file $file_path$new_file_base"
  113.     fi
  114.     if [[ -n ${name_only} ]]
  115.     then
  116.     print -- $file_path$new_file_base
  117.     fi
  118.  
  119.     ${no_execute} mv $file $file_path$new_file_base
  120.  
  121.     exit_status=$?
  122.     if [[ $exit_status != 0 ]]
  123.     then
  124.     print -u2 "${0##*/}:mv failed on '$file' with exit status $exit_status"
  125.     if (( $# == 1 ))
  126.         then
  127.         print -u2 "1 file left: $@ "
  128.     elif (( $# != 0 ))
  129.         then
  130.         print -u2 "$# files left: $@ "
  131.         fi 
  132.     exit exit_status
  133.     fi
  134.     done
  135. exit 0
  136.