csh programming problem

Dan Ellard ellard at bbn.com
Tue Mar 13 23:44:09 AEST 1990


I am trying to write a shell script that takes several arguments and
passes them to child processes, i.e.

#!/bin/csh
foo $1 $2 ...
bar $1 $2 ...
exit 0

which I want to have the same effect as if I had typed to the shell:
foo $1 $2 ...
bar $1 $2 ...

Unfortunately, this method does not work when $1 is something like 'a b', since
$1 evaluates to the string ( a b ) without the quotes, so foo and bar
think that their first argument is 'a' and their second argument is 'b',
rather than that their first argument is 'a b'.

My solution to this problem is to build a new argument list, "requoting" each
argument, using this ugly piece of code:

#!/bin/csh
set count	= 1
set args	= $#argv
set new_args	= ""
while ($count <= $#argv)
	set  new_args = ( $new_args \'$argv[$count]\' )
	set count = `expr $count + 1`
end
foo $new_args
bar $new_args
exit 0

(I am open to suggestions concerning how to improve this)

This solves the original problem, but there are still more problems to
solve--  like handling quoted characters.  If I call this program with
$1 as \'foo, the backslash will be peeled off and my quoting scheme will
break.  Similar problems will arise if I try to use other characters that
have special meaning to the shell.  I could get around this by writing a
simple C program that re-inserts backslashes as necessary, but this seems
very clumsy.  Is there a good way to do this using either csh or sh, or
should I give up and write a C program to do it?

Thanks,
	Dan

---------------------------------------------------------------------------
Dan Ellard -- ellard at bbn.com
This line intentionally not left blank.



More information about the Comp.unix.wizards mailing list