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

  1. #!/bin/ksh
  2. # @(#) filt.ksh 2.0 96/11/09
  3. # 92/10/08 john h. dubois iii (john@armory.com)
  4. # 96/11/09 Added all options.
  5.  
  6. # Use ksh because sh "type" doesn't exit nonzero if it fails
  7. # & ksh has continue <n>
  8.  
  9. name=${0##*/}
  10. Usage="Usage: $name [-efhcnv] [-f<file>] <filter> [<file> ...]"
  11. overwrite=false
  12. backup=true
  13. check=true
  14. eval=
  15. filesGiven=false
  16. verbose=false
  17.  
  18. while getopts eohcnf:v opt; do
  19.     case $opt in
  20.     h)
  21.     print -r -- \
  22. "$name: filter files.
  23. Each <file> is passed to <filter> as its standard input.
  24. Output is written to <file>+.  If the exit value of <filter> is zero,
  25. the original file is moved <file>- and the output is moved to <file>.
  26. $Usage
  27. Options:
  28. -h: Print this help.
  29. -v: Be verbose.
  30. -e: \"eval\" the filter command.  This is neccessary if it contains
  31.     characters that must be interpreted by the shell, like quoting characters.
  32.     Example: $name -e \"grep 'foo bar'\" file
  33. -f<file>: Specify a file to be filtered.  If a filename is given with -f, the
  34.     remaining (non-option) arguments are all taken to be part of the filter
  35.     command.  This is an alternative to the -e option for specifying complex
  36.     filter commands.  Multiple -f arguments may be given.
  37. -o: Normally $name will skip a file if <file>+ or (if -n is not given) <file>-
  38.     already exists.  If -f is given, they will be overwritten.
  39. -c: After a successful filter, instead of moving files, copy them: <file>
  40.     is copied to <file>-, <file> is overwritten with the contents of <file>+,
  41.     and then <file>+ is removed.  This preserves the original inode of <file>.
  42. -n: Do not keep a backup copy of <file> in <file>-."
  43.     exit 0
  44.     ;;
  45.     f)
  46.     set -A files -- "${files[@]}" "$OPTARG"
  47.     filesGiven=true
  48.     ;;
  49.     e)
  50.     eval=eval
  51.     ;;
  52.     c)
  53.     overwrite=true
  54.     ;;
  55.     o)
  56.     check=false
  57.     ;;
  58.     n)
  59.     backup=false
  60.     ;;
  61.     v)
  62.     verbose=true
  63.     ;;
  64.     ?)
  65.     print -r -u2 "Use -h for help."
  66.     exit 1
  67.     ;;
  68.     esac
  69. done
  70.  
  71. # remove args that were options
  72. let OPTIND=OPTIND-1
  73. shift $OPTIND
  74.  
  75. if [ $# -lt 2 ]; then
  76.     print -r -u2 -- "$Usage
  77. Use -h for help."
  78.     exit 1
  79. fi
  80.  
  81. if $filesGiven; then
  82.     set -A filter -- "$@"
  83. else
  84.     tm=$1
  85.     shift
  86.     set -A files -- "$@"    # some ksh lose positional param when -A used
  87.     set -A filter -- $tm
  88. fi
  89.  
  90. type "${filter[0]}" >/dev/null 2>&1 || {
  91.     print -ru2 -- "Cannot execute filter command: ${filter[0]}"
  92.     exit 1
  93. }
  94.  
  95. for file in "${files[@]}"; do
  96.     if $check; then
  97.     if [ -f "$filt+" ]; then
  98.         print -ru2 -- "$filt+ already exists.  $file not processed."
  99.         continue
  100.     fi
  101.     if $backup && [ -f "$file-" ]; then
  102.         print -ru2 -- "$file- already exists.  $file not processed."
  103.         continue
  104.     fi
  105.     fi
  106.     if [ ! -r "$file" ]; then
  107.     print -ru2 -- "$file: cannot read."
  108.     continue
  109.     fi
  110.     $verbose && print -r -- "Filtering $file..."
  111.     if $eval "${filter[@]}" < "$file" > "$file+"; then
  112.     if $backup; then
  113.         $verbose && print -r -- "Backing up $file..."
  114.         $overwrite && cp -- "$file" "$file-" || mv -- "$file" "$file-"
  115.         if [ $? -ne 0 ]; then
  116.         print -ru2 -- \
  117.         "Backup of $file to $file- failed; filtered contents left in $file+"
  118.         exit 1
  119.         fi
  120.     fi
  121.     if $verbose; then
  122.         print -r -- "Old file:"
  123.         l -- "$file"
  124.         print -r -- "After filtering:"
  125.         l -- "$file+"
  126.         print -r -- "Replacing contents of $file with filtered contents..."
  127.     fi
  128.     $overwrite && cat -- "$file+" > "$file" || mv -- "$file+" "$file"
  129.     if [ $? -ne 0 ]; then
  130.         print -ru2 -- \
  131. "Move of filtered contents to $file back to $file failed;
  132. contents of $file may be inconsistent!!"
  133.         exit 1
  134.     fi
  135.     if $overwrite; then
  136.         $verbose && print -r -- "Removing $file+..."
  137.         rm -- "$file+"
  138.     fi
  139.     print -r -- "$file succesfully filtered."
  140.     else
  141.     print -r -- "filter exited nonzero on file $file; $file not touched."
  142.     fi
  143. done
  144.