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

  1. #!/bin/ksh
  2. # @(#) nfile.ksh 1.1 95/06/06
  3. # 93/10/05 john h. dubois iii (john@armory.com)
  4. # 94/04/27 Give undocumented -a option to strings to tell it not to do
  5. #          lame OMF-header-reading that makes it fail for pipes
  6. # 94/05/22 Use ^A for sed sub. since / is quite likely to appear in paths
  7. # 95/01/31 Generalized from wstrings.
  8. # 95/06/06 Avoid exec if possible.  Added [ir] options.
  9.  
  10. # If not being interpreted by ksh, exec it.
  11. # The advantage of doing it this way is that if the user is running ksh,
  12. # this script will be run without any exec (only a fork), allowing any amount
  13. # of command line arguments to be passed.
  14. #[ -z "$SECONDS" ] && exec /bin/ksh "$0" "$@"
  15.  
  16. name=${0##*/}
  17. Usage="Usage: $name [-hir] <command> [-options] [filename ...]"
  18.  
  19. ReadInput=false
  20. RegOnly=false
  21.  
  22. while getopts :hir opt; do
  23.     case $opt in
  24.     h)
  25.     echo \
  26.     print \
  27. "$name: invoke a command on files and add filenames to its output.
  28. $Usage
  29. <command> is run on each named file in turn.  The output of <command> is
  30. processed to prefix each line with the name of the file passed to it.
  31. Any arguments after <command> that begin with '-' are taken to be options
  32. to <command> and are passed to each instance of it.
  33. If no filenames are given, <command> is run once without filename arguments.
  34. Options:
  35. -h: Print this help.
  36. -i: Read the input for a list of filenames to process, one per line.
  37.     If this option is given, all arguments after <command> are taken to be
  38.     options to be passed to each instance of <command>.
  39. -r: Only process regular files (skip others silently)."
  40.     exit 0
  41.     ;;
  42.     i)
  43.     ReadInput=true
  44.     ;;
  45.     r)
  46.     RegOnly=true
  47.     ;;
  48.     +?)
  49.     print -u2 "$name: options should not be preceded by a '+'."
  50.     exit 1
  51.     ;;
  52.     :) 
  53.     print -r -u2 -- \
  54.     "$name: Option '$OPTARG' requires a value.  Use -h for help."
  55.     exit 1
  56.     ;;
  57.     ?) 
  58.     print -u2 "$name: $OPTARG: bad option.  Use -h for help."
  59.     exit 1
  60.     ;;
  61.     esac
  62. done
  63.  
  64. # remove args that were options
  65. let OPTIND=OPTIND-1
  66. shift $OPTIND
  67.  
  68. if [ $# -eq 0 ]; then
  69.     print -u2 \
  70. "$name: not enough arguments.
  71. $Usage
  72. Use -h for help."
  73.     exit 1
  74. fi
  75.  
  76. [ "$1" = strings ] && command="strings -a" || command=$1
  77. shift
  78.  
  79. if $ReadInput; then
  80.     set -A args -- "$@"
  81. else
  82.     typeset -i narg=0
  83.     while [[ "$1" = -* ]]; do
  84.     let narg+=1
  85.     args[narg]=$1
  86.     shift
  87.     done
  88. fi
  89.  
  90. function ProcFile {
  91.     typeset file=$1
  92.  
  93.     $RegOnly && [ ! -f "$file" ] && continue
  94.     $command "${args[@]}" "$file" | sed "s^$file: "
  95. }
  96.  
  97. if $ReadInput; then
  98.     while read file; do
  99.     ProcFile "$file"
  100.     done
  101. elif [ $# -gt 0 ]; then
  102.     for file; do
  103.     ProcFile "$file"
  104.     done
  105. else
  106.     $command "${args[@]}" | sed "s/^/stdin: /"
  107. fi
  108.