command line arguments

John B. Chambers jbc at mcc-db.UUCP
Mon Jul 15 09:56:22 AEST 1985


From: John Chambers (guest moderator) <ut-sally!std-unix>

Topic: command line arguments

----------------------------------------------------------------------

Date: Mon, 8 Jul 85 00:52:46 pdt
From: nsc!turtlevax!ken at 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 <outfile>	Output directed to <outfile> instead of default
	<file> ...	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 <outfile>	Change output file to <outfile> instead of the default.
	-? or -help	Prints this help message
	<file> ...	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 at DECWRL.ARPA


----------------------------------------------------------------------

-- 

John B. Chambers, Microelectronics and Computer Technology Corp., Austin, TX
{ihnp4,seismo,ctvax}!ut-sally!mcc-db!jbc, jbc at ut-sally.ARPA, chambers at mcc.ARPA



More information about the Mod.std.unix mailing list