Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 9 Apr 2014 12:41:30 -0700
From:      <dteske@FreeBSD.org>
To:        "'Gary Aitken'" <garya@dreamchaser.org>, "'FreeBSD Mailing List'" <freebsd-questions@freebsd.org>
Subject:   RE: blindly passing on shell flags ($- )?
Message-ID:  <0f3901cf542b$b58e4300$20aac900$@FreeBSD.org>
In-Reply-To: <53459F47.6080407@dreamchaser.org>
References:  <53459F47.6080407@dreamchaser.org>

next in thread | previous in thread | raw e-mail | index | archive | help


> -----Original Message-----
> From: Gary Aitken [mailto:garya@dreamchaser.org]
> Sent: Wednesday, April 9, 2014 12:28 PM
> To: FreeBSD Mailing List
> Subject: blindly passing on shell flags ($- )?
> 
> As I read the man page for sh and bash, the variable $- is supposed to
> represent the flags passed to the script.  However, whenever I look at it
it is
> empty.
> 

$- represents the flags set with the "set" command, not positional
arguments.
For positional arguments, see either "$@" or "$*".

> Reading between the lines, it appears one may have to call getopts so the
> shell knows what the flags are, but that doesn't seem to change what's
> available in $-.
> 

getopts operates on positional arguments ($@ or $*) not option-flags.
Perhaps confusing is that the "set" command is used in modification of
either option-flags (primary function) or positional arguments (arguably
secondary function).

For example, changing an option-flag (which would change value of $-)

	set -e

And example for changing positional arguments (post-invocation; having
an effect on the $@ or $* variables)

	set -- arg1 arg2 arg3 ...


> Fundamentally, I want to add an arg (not a flag) and pass the whole
shebang
> on to another script.  Seems like this should be trivial but I can't get
it to work.

You would use "$@" in passing positional arguments to another script (this
will ensure proper expansion so each positional argument is passed as-is;
unlike "$*" which will join them together separated by the internal field
separator
aka $IFS).

> The flags seem to be treated as a normal arg.
> The only way I seem to be able to get what I want is to interpret all the
flags
> in the first script and pass them explicitly to the second, which is what
I'm
> trying to avoid.
> 
> foo:
>   #!/bin/sh
>   echo foo here
>   echo flags $-
>   echo args $1 $2
>   bar $- INSERTED $1 $2
>   getopts ah flag
>   bar $- INSERTED $1 $2
> 

> bar:
>   #!/bin/sh
>   echo bar here
>   echo flags $-
>   echo args $1 $2 $3
> 
> $foo -a abc
> foo here
> flags
> args -a abc
> bar here
> flags
> args INSERTED -a abc
> bar here
> flags
> args INSERTED -a abc
> 
> What am I missing?
> 

Use "$@" instead of "$-" -- flags are not args.
-- 
Devin

_____________
The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?0f3901cf542b$b58e4300$20aac900$>