[comp.unix.questions] Command line argument in Cshell script

saito@slb-sdr.UUCP (Naoki Saito) (06/03/88)

	Hello, I wrote a C-shell script to automate the task as follows:
=======================================================================
#
# Shell script for the plot3d for field files.
#
# Usage: p3d <filename> [parameters for plot3d]
#

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
=======================================================================

	This works fine unless I use command line arguments of strings which
contain space, e.g.,
(1) p3d fname tl="This_is_wrong" ---> OK
(2) p3d fname tl="This is wrong" ---> Failed

This means that in the case of (1) $argv[2] is considered as tl="This_is_wrong"
but in (2) $argv[2] becomes tl="This.
	How can I pass the space containing arguments? Does anybody out there
have solution for this?

Regards,




-- 
Naoki Saito (saito@sdr.slb.com)
Schlumberger-Doll Research

jeff@unh.UUCP (Jeffrey E. F. Friedl) (06/04/88)

In article <497@slb-sdr.UUCP>, saito@slb-sdr.UUCP (Naoki Saito) writes:
> 
> 	Hello, I wrote a C-shell script to automate the task as follows:
> =======================================================================
> #
  [start of program]
> plot3d z=$TEMP -P $argv[2-] | sunplot
  [rest of program]
> 
> 	This works fine unless I use command line arguments of strings which
> contain space, e.g.,
> (1) p3d fname tl="This_is_wrong" ---> OK
> (2) p3d fname tl="This is wrong" ---> Failed
> 

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.

Also, to load faster, have the first line be
#/bin/csh -f

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

	*jeff*
-------------------------------------------------------------------------------
Jeffrey Eric Francis Friedl, Box 2146 Babcock House, Durham New Hampshire 03824
..!{uunet,decvax}!unh!jeff   BITNET%"j_friedl@unhh"  ..!ucbvax!kentvax!jfriedl

I hope I'm not around Jan 18, 2038 at 10:14:08PM

(friedl@vsi is my brother, and I'm proud of it. He is too [even if he says no]).

chris@mimsy.UUCP (Chris Torek) (06/06/88)

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

In article <534@unh.UUCP> jeff@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@mimsy.umd.edu	Path:	uunet!mimsy!chris

leo@philmds.UUCP (06/08/88)

In article <497@slb-sdr.UUCP> saito@slb-sdr.UUCP (Naoki Saito) writes:
>	Hello, I wrote a C-shell script to automate the task as follows:
     [stuff deleted]   
>plot3d z=$TEMP -P $argv[2-] | sunplot
     [stuff deleted]   
>	
>	This works fine unless I use command line arguments of strings which
>contain space, e.g.,
     [stuff deleted]   

	I'm not a csh expert (use a Bourne sh variant myself) but I think I can
see the problem. When you start the script, you correctly quote the string
argument that contains spaces. So the script gets it in its $argv[2]. However,
when you execute the plot3d command from within the script, you do not quote
it. Why not? I think the csh now offers the words of the strings as separate
arguments to plot3d. Quote them too (in sh you would need "" so that the 
value of $argv[2] is used).

	Leo.