home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / shorten < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  2.0 KB  |  69 lines

  1. #!/bin/ksh
  2. # @(#) shorten.ksh 1.1 97/03/26
  3. # 92/10/24 john h. dubois iii (john@armory.com)
  4. # 92/12/05 changed -<maxlength> option to -m<maxlength>
  5. # 97/03/26 Fixed calculation done for -aA
  6.  
  7. name=${0##*/}
  8. Usage="Usage: $name [-ht] [-m<maxlength>|-[aA]<names-across>] filename ..."
  9. length=23
  10. cmd=mv
  11.  
  12. while getopts :hta:A:m: opt; do
  13.     case $opt in
  14.     h) echo \
  15. "${0##*/}: shorten filenames.
  16. ${0##*/} truncates filenames to a given maximum length.  The default is 23.
  17. ${0##*/} will not overwrite files when renaming.
  18. $Usage
  19. Options:
  20. -m<maxlength>: truncate filenames to <maxlength> characters.
  21. -a<names-across>: truncate filenames to the maximum length that will
  22.    allow lf to print <names-across> filenames across an 80-column display.
  23. -A<names-across>: same as -a except that if the environment variable COLUMNS
  24.    is set it is used for the display width.
  25. -t: test.  ${0##*/} reports what it would do but does not rename any files.
  26. -h: print this help."
  27.        exit 0;;
  28.     # lf uses this formula which puts 2 unneeded spaces at the end of the line.
  29.     a) length="80/$OPTARG-3";;
  30.     A) [ -z "$COLUMNS" ] && COLUMNS=80
  31.        length="$COLUMNS/$OPTARG-3";;
  32.     m) length=$OPTARG;;
  33.     t) cmd="echo mv";;
  34.     +?) echo "$name: options should not be preceded by a '+'."; exit 1;;
  35.     :) 
  36.     print -r -u2 -- \
  37.     "$name: Option '$OPTARG' requires a value.  Use -h for help."
  38.     exit 1
  39.     ;;
  40.     ?) echo "$name: $OPTARG: bad option.  Use -h for help."; exit 1;;
  41.     esac
  42. done
  43.  
  44. # remove args that were options
  45. let OPTIND=OPTIND-1
  46. shift $OPTIND
  47.  
  48. # If a non-number was given for a numeric option,
  49. # this will make length empty.
  50. typeset -i length
  51.  
  52. if [ $# -lt 1 -o length -eq 0 ]; then
  53.     echo "$Usage"
  54.     exit 1
  55. fi
  56.  
  57. typeset -L$length fixwid
  58.  
  59. for file; do
  60.     fixwid=$file
  61.     newname=${fixwid%%*( )}    # get rid of space padding done by typeset -L
  62.     [ "$file" = "$newname" ] && continue
  63.     if [[ -a "$newname" ]]; then
  64.     print -u2 "$newname exists.  $file not renamed."
  65.     continue
  66.     fi
  67.     $cmd "$file" "$newname"
  68. done
  69.