[comp.lang.c] Unseting positional parmaters from inside a program

covertr@force.UUCP (Richard E. Covert) (01/19/90)

Does anyone know of a way to unset the positional parameters
from inside of a C program??

I have a program(setargs) in which I parse the command line. The output
of the program is captured in a Bourne shell script and saved in
a variable. The variable is then 'eval'ed. This allows me to assign
values to variables from within the shell script. The values are
builtin to the script returned from my program. AS an example:

UNIX>test_setargs -a -b "bs string"


____________________________ test_setargs __________________
#set -xv
args=`./setargs "ab:" "avar:bvar" "$@" `
#                ^^^^  ^^^^^^^^^
# like getopt() only setargs allows you to give values to variables
#
echo "args is $args"
echo "before the eval args statement"

echo "avar=$avar"
echo "bvar=$bvar"
echo "postparm1 = $1"
echo "postparm2 = $2"

eval $args

echo "after the eval args statement"


echo "avar=$avar"
echo "bvar=$bvar"
echo "postparm1 = $1"
echo "postparm2 = $2"


_______________ screen dump of test_setargs __________________
args is avar=1;bvar='bs string';set -- 
before the eval args statement
avar=
bvar=
postparm1 = -a
postparm2 = -b
after the eval args statement
avar=1
bvar=bs string
postparm1 = -a
postparm2 = -b
______________________________________________________________________
  
args is a shell variable containing the string out of my program. But, as you
see "avar" and "bvar" are set to their proper value but the positional p
parameters $1, and $2 still contain "-a" and "-b". The whole purpose of setargs
is to generate a string which is evaluated and sets values for variables.
But, I would like to clear $1 and $2 so that after running setargs $1 and
$2 would be cleared (as they were processed by setargs). So, if setargs processed all
of its positional parameters then the shell wouldn't have any values for $1, $2
etc after executing setargs.

Kinda complicated but setargs is great for parsing lines for variables.



-- 
 Richard E. Covert, Lead Engineer of Software Tools Group
 AG Communications Systems, Phoenix AZ   (602) - 581-4652
 TCP/IP: covertr@gtephx
 UUCP: {ncar!noao!asuvax | uunet!zardoz!hrc | att}!gtephx!covertr

maart@cs.vu.nl (Maarten Litmaath) (01/23/90)

In article <481d9fde.14a1f@force.UUCP>,
	covertr@force.UUCP (Richard E. Covert) writes:
\...
\args=`./setargs "ab:" "avar:bvar" "$@" `
\...
\_______________ screen dump of test_setargs __________________
\args is avar=1;bvar='bs string';set -- 
\...

The problem:

	set --

doesn't unset the arguments (set $# to 0), it does nothing, which is a bug
(dubious feature) IMHO.  Solution:

	set x <the remaining arguments, if any>
	shift

Followups to comp.unix.questions.
-- 
  What do the following have in common:  access(2), SysV echo, O_NONDELAY?  |
  Maarten Litmaath @ VU Amsterdam:  maart@cs.vu.nl,  uunet!mcsun!botter!maart