Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/5/84; site mcc-db.UUCP Path: utzoo!decvax!genrad!panda!talcott!harvard!seismo!ut-sally!mcc-db!jbc From: jbc@mcc-db.UUCP (John B. Chambers) Newsgroups: mod.std.unix Subject: Re: command line arguments Message-ID: <245@mcc-db.UUCP> Date: Sun, 14-Jul-85 19:56:22 EDT Article-I.D.: mcc-db.245 Posted: Sun Jul 14 19:56:22 1985 Date-Received: Mon, 15-Jul-85 10:04:56 EDT Reply-To: std-unix-request@ut-sally Organization: MCC (Austin, TX) Lines: 155 Approved: jbc@mcc-db.UUCP From: John Chambers (guest moderator)Topic: command line arguments ---------------------------------------------------------------------- Date: Mon, 8 Jul 85 00:52:46 pdt From: nsc!turtlevax!ken@ihnp4.UUCP (Ken Turkowski) Subject: Re: command line arguments Someone suggested that parsing arguments in shell scripts was difficult. I include the following shell scripts, one for the Bourne shell and one for the C-shell, which parse arguments of the form: -v -O -o outfile file1 file2 file3 as well as -vOooutfile file1 file2 file3 ================================================================= # This is a shell archive. Remove anything before this line, then # unpack it by saving it in a file and typing "sh file". (Files # unpacked will be owned by you and have default permissions.) # # This archive contains: # std.sh std.csh echo x - std.sh cat > "std.sh" << '//E*O*F std.sh//' #! /bin/sh PATH=/usr/ucb:/bin:/usr/bin:/usr/local/bin verbose=false files= trap 'rm $temp_files; exit' 0 1 2 for arg # Look for flags do while true do case $arg in -\?|-help) cat << EOF This shell script accepts the arguments: -v Verbose -o Output directed to instead of default ... Files to be processed It parses the arguments and prints the results of the parse. EOF exit ;; -) echo NULL FLAG; break ;; -v*) verbose=true ;; -o*) outfile=`expr $arg : '-.\(.*\)'` || outfile=UNKNOWN break ;; -*) echo Unknown flag: \"$arg\" unkflag="$unkflag $arg" break ;; *) case UNKNOWN in $outfile) outfile=$arg ;; *) files="$files $arg" ;; esac break ;; esac arg=-`expr "$arg" : '-.\(.*\)'` || break done done set x $files shift for arg # input file processing template do echo processing file \"$arg\" done # The following is just for testing this standard script echo ' Argument' analysis to $0: echo Flags: -v = $verbose, -o = \"$outfile\" echo Unknown flags: $unkflag echo Files: $files //E*O*F std.sh// echo x - std.csh cat > "std.csh" << '//E*O*F std.csh//' #! /bin/csh -f unalias * set path = (/usr/ucb /bin /usr/bin /usr/local/bin) unset Verbose set temp_files= set outfile set unkflag set files onintr cleanup foreach argument ( $*:q ) while ( 1 ) switch ( "$argument" ) case -[?]: case -help: cat << EOF This shell script take the arguments: -v Verbose -o Change output file to instead of the default. -? or -help Prints this help message ... Input files It parses them and prints the result of the parse. EOF exit case -: echo 'NULL FLAG' break case -v*: set Verbose breaksw case -o*: set outfile = `expr $argument : '-.\(.*\)'` || set outfile = UNKNOWN break case -*: echo Unknown flag: \""$argument"\" set unkflag = "$unkflag $argument" break case *: switch ( UNKNOWN ) case "$outfile": set outfile = $argument breaksw default: set files = "$files $argument" endsw break endsw set argument = -`expr "$argument" : '-.\(.*\)'` || break end end # The following is just for testing this standard script echo ' Argument analysis to' $0 ':' echo Flags: -v = ${?Verbose}, -o = \"$outfile\" echo Unknown flags: $unkflag echo Files: $files cleanup: rm $temp_files exit //E*O*F std.csh// exit 0 -- Ken Turkowski @ CADLINC, Menlo Park, CA UUCP: {amd,decwrl,hplabs,nsc,seismo,spar}!turtlevax!ken ARPA: turtlevax!ken@DECWRL.ARPA ---------------------------------------------------------------------- -- John B. Chambers, Microelectronics and Computer Technology Corp., Austin, TX {ihnp4,seismo,ctvax}!ut-sally!mcc-db!jbc, jbc@ut-sally.ARPA, chambers@mcc.ARPA