Command line argument in Cshell script

Chris Torek chris at mimsy.UUCP
Mon Jun 6 07:28:43 AEST 1988


>In article <497 at slb-sdr.UUCP> saito at slb-sdr.UUCP (Naoki Saito) writes:
[much deleted]
>>plot3d z=$TEMP -P $argv[2-] | sunplot

In article <534 at unh.UUCP> jeff at unh.UUCP (Jeffrey E. F. Friedl) writes:
>Put quotes such as:
>original:	plot3d z=$TEMP -P  $argv[2-]  | sunplot
>working:	plot3d z=$TEMP -P "$argv[2-]" | sunplot
>
>Thus, when $argv[2] is expanded, it is expanded within quotes and is considered
>one arg to plot3d.

But `$argv[2-]' means `arguments 2 through $#argv'; quoting this will
give a single word rather than multiple words, if $#argv > 2.  If this
is not wanted (as it apparently is not), use the :q modifier:

	plot3d z=$TEMP -P $argv[2-]:q | sunplot

>... most shell scripts should be written in [k]?sh............

Seconded.  Here is the original script:

	set TEMP=/tmp/z
	if (-e $TEMP) 	\rm $TEMP

	chkf -b -d $argv[1] > $TEMP
	plot3d z=$TEMP -P $argv[2-] | sunplot
		
	if (-e $TEMP) 	\rm $TEMP

	exit

Here is how I might write it:

	case $# in
	0)	echo "usage: $0 file [arguments to plot3d]" 1>&2; exit 1;;
	esac

	TEMP=/tmp/z$$			# make a unique temporary file name
	/bin/rm -f $TEMP		# remove it if it exists
	trap '/bin/rm -f $TEMP; exit' 0 1 2 3 15 # and again at exit or signal

	file="$1"; shift		# pick up file name
	chkf -b -d "$file" > $TEMP	# run chkf
	plot3d z=$TEMP -P ${1+"$@"}	# plot, with optional arguments
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.unix.wizards mailing list