[comp.unix.questions] mulltiword arguments in C-shell

strong@tc.fluke.COM (Norm Strong) (10/27/87)

I have a problem:

	I'm writing a C-shell script which reads in arguments from the command
line, shifts them around in a prearranged fashion, and spits them back out
again.  The problems is that some of these arguments have more than one word in
them.  Such arguments have quotes around them, i.e. "boo hoo".

	When the shell reads these arguments into the program it recognizes the
quoted arguments as a single argument--but--it promptly removes the quotes, and
the argument is thereafter handled as 2 arguments.

	My question, Mr. Anthony, is:  How can I get the shell to handle these
multi-word arguments all the way through to the output and dump them out with
the quotes still around them, so that the next program will also recognize them
as a single argument?

Help!

Norm (strong@fluke)

wagner@rocky.STANFORD.EDU (Juergen Wagner) (10/28/87)

Why don't you use quotes in the output? Computing or selecting the
values from $argv you want to keep can be done by building an index
list $index. Later there is some output loop:
	foreach i ( $indices )
		echo -n \"$argv[$i]\"" "
	end

Juergen Wagner,			(USENET) gandalf@portia.stanford.edu
Center for the Study of Language and Information (CSLI), Stanford CA

rwhite@nusdhub.UUCP (10/28/87)

In article <2100@sputnik.COM>, strong@tc.fluke.COM (Norm Strong) writes:
> 	My question, Mr. Anthony, is:  How can I get the shell to handle these
> multi-word arguments all the way through to the output and dump them out with
> the quotes still around them, so that the next program will also recognize them
> as a single argument?

The simple way would be to use 

echo \"${ARG1}\" \"${ARG2}\"

you get the idea....  "If you want the \"(s) you gotta \\ (quote) the quotes"

Rob.

schaefer@ogcvax.UUCP (Barton E. Schaefer) (10/28/87)

In article <sputnik.2100> strong@tc.fluke.COM (Norm Strong) writes:
>I have a problem:
>	My question, Mr. Anthony, is:  How can I get the shell to handle these
>multi-word arguments all the way through to the output and dump them out with
>the quotes still around them, so that the next program will also recognize them
>as a single argument?
>
>Norm (strong@fluke)

This is an EASY one!

The csh supports a number of "colon modifiers" that can be applied to variables
and history substitutions to either cause or prevent substitutions.  The one
you want is ":q" (for "quote"), which (among other things) prevents separation
of a variable into words when it is referenced.  To see how this works, try the
following (remember that $#var is replaced by the number of words in var):

	% set one="This is one string"
	% echo $#one
	1
	% set four=($one)
	% echo $#four
	4
	% set onetoo=($one:q)
	% echo $#onetoo
	1
	%

In a csh script, colon modifiers can be appended to any of the "positional"
variables ($1, $2, $3, etc) EXCEPT $0 (the script file name).  You must also
remember to use :q whenever you reference any variable that is an aggregate
of quoted variables.  For example,

	# Separate option flags and other arguments
	set opts=() others=()
	while ($#argv)
	    switch ($1)
	    case -* :
		# note :q on ref to $opts
		set opts=($opts:q $1:q)
		breaksw
	    default :
		# similarly for $others
		set others=($others:q $1:q)
	    endsw
	    shift
	end

A quick summary of colon modifiers (from 4.3 BSD csh manual)

    Modifier	Result				Usable in substitution
    -------	------				----------------------
    h		Take head of path		History or variable
    t		Take tail of path		History or variable
    r		Take root of name		History or variable
    e		Take extension (.xxx) of name	History or variable[*]
    s/p/r/	Substitute r for p		History only
    &		Repeat previous substitution	History only
    gX		Apply modifier X globally	Where X is usable
    p		Print but don't execute		History only
    q		Quote				History or variable
    x		Like q but make words		History or variable

[*] The csh manual omits this for variables, but it works on my system

Only ONE colon modifier can currently be used on any substitution.

-- 
Bart Schaefer			CSNET:	schaefer@cse.ogc.edu
				UUCP:	...{tektronix,verdix}!ogcvax!schaefer
"It could have been worse.  I could have woken up and been Robert Bork."
	    -- Joe Garagiola, after his broadcast partner angered Twins fans