home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / std_unix / mod.std.unix.v1 / text0022.txt < prev    next >
Encoding:
Internet Message Format  |  1987-06-30  |  3.9 KB

  1. From: John Chambers (guest moderator) <ut-sally!std-unix>
  2.  
  3. Topic: command line arguments
  4.  
  5. ----------------------------------------------------------------------
  6.  
  7. Date: Mon, 8 Jul 85 00:52:46 pdt
  8. From: nsc!turtlevax!ken@ihnp4.UUCP (Ken Turkowski)
  9. Subject: Re: command line arguments
  10.  
  11. Someone suggested that parsing arguments in shell scripts was difficult.
  12. I include the following shell scripts, one for the Bourne shell and one
  13. for the C-shell, which parse arguments of the form:
  14.     -v -O -o outfile file1 file2 file3
  15. as well as
  16.     -vOooutfile file1 file2 file3
  17.  
  18. =================================================================
  19.  
  20. # This is a shell archive.  Remove anything before this line, then
  21. # unpack it by saving it in a file and typing "sh file".  (Files
  22. # unpacked will be owned by you and have default permissions.)
  23. #
  24. # This archive contains:
  25. # std.sh std.csh
  26.  
  27. echo x - std.sh
  28. cat > "std.sh" << '//E*O*F std.sh//'
  29. #! /bin/sh
  30. PATH=/usr/ucb:/bin:/usr/bin:/usr/local/bin
  31. verbose=false
  32. files=
  33. trap 'rm $temp_files; exit' 0 1 2
  34. for arg            # Look for flags
  35. do    while true
  36.     do    case $arg in
  37.         -\?|-help) cat << EOF
  38. This shell script accepts the arguments:
  39.     -v        Verbose
  40.     -o <outfile>    Output directed to <outfile> instead of default
  41.     <file> ...    Files to be processed
  42. It parses the arguments and prints the results of the parse.
  43. EOF
  44.             exit ;;
  45.         -)    echo NULL FLAG; break ;;
  46.         -v*)    verbose=true ;;
  47.         -o*)    outfile=`expr $arg : '-.\(.*\)'` || outfile=UNKNOWN
  48.             break ;;
  49.         -*)    echo Unknown flag: \"$arg\"
  50.             unkflag="$unkflag $arg"
  51.             break ;;
  52.         *)    case UNKNOWN in
  53.             $outfile)    outfile=$arg ;;
  54.             *)        files="$files $arg" ;;
  55.             esac
  56.             break ;;
  57.         esac
  58.         arg=-`expr "$arg" : '-.\(.*\)'` || break
  59.     done
  60. done
  61.  
  62. set x $files
  63. shift
  64. for arg        # input file processing template
  65. do
  66.     echo processing file \"$arg\"
  67. done
  68.  
  69. # The following is just for testing this standard script
  70. echo '
  71. Argument' analysis to $0:
  72. echo Flags: -v = $verbose, -o = \"$outfile\"
  73. echo Unknown flags: $unkflag
  74. echo Files: $files
  75. //E*O*F std.sh//
  76.  
  77. echo x - std.csh
  78. cat > "std.csh" << '//E*O*F std.csh//'
  79. #! /bin/csh -f
  80. unalias *
  81. set path = (/usr/ucb /bin /usr/bin /usr/local/bin)
  82. unset Verbose
  83. set temp_files=
  84. set outfile
  85. set unkflag
  86. set files
  87. onintr cleanup
  88. foreach argument ( $*:q )
  89.     while ( 1 )
  90.         switch ( "$argument" )
  91.         case -[?]:
  92.         case -help:
  93.         cat << EOF
  94. This shell script take the arguments:
  95.     -v        Verbose
  96.     -o <outfile>    Change output file to <outfile> instead of the default.
  97.     -? or -help    Prints this help message
  98.     <file> ...    Input files
  99. It parses them and prints the result of the parse.
  100. EOF
  101.         exit
  102.             case -:
  103.                 echo 'NULL FLAG'
  104.                 break
  105.             case -v*:
  106.         set Verbose 
  107.         breaksw
  108.             case -o*:    
  109.                 set outfile = `expr $argument : '-.\(.*\)'` || set outfile = UNKNOWN
  110.                 break
  111.             case -*:    
  112.                 echo Unknown flag: \""$argument"\"
  113.                 set unkflag = "$unkflag $argument"
  114.                 break
  115.             case *:    
  116.                 switch ( UNKNOWN )
  117.                     case "$outfile":
  118.             set outfile = $argument
  119.             breaksw
  120.                     default:        
  121.             set files = "$files $argument" 
  122.                 endsw
  123.                 break
  124.         endsw
  125.         set argument = -`expr "$argument" : '-.\(.*\)'` || break
  126.     end
  127. end
  128.  
  129. # The following is just for testing this standard script
  130. echo ' Argument analysis to' $0 ':'
  131. echo Flags: -v = ${?Verbose}, -o = \"$outfile\"
  132. echo Unknown flags: $unkflag
  133. echo Files: $files
  134.  
  135. cleanup:
  136.     rm $temp_files
  137. exit
  138. //E*O*F std.csh//
  139.  
  140. exit 0
  141. -- 
  142.  
  143. Ken Turkowski @ CADLINC, Menlo Park, CA
  144. UUCP: {amd,decwrl,hplabs,nsc,seismo,spar}!turtlevax!ken
  145. ARPA: turtlevax!ken@DECWRL.ARPA
  146.  
  147.  
  148. ----------------------------------------------------------------------
  149.  
  150. -- 
  151.  
  152. John B. Chambers, Microelectronics and Computer Technology Corp., Austin, TX
  153. {ihnp4,seismo,ctvax}!ut-sally!mcc-db!jbc, jbc@ut-sally.ARPA, chambers@mcc.ARPA
  154.  
  155. Volume-Number: Volume 1, Number 23
  156.  
  157.