From owner-freebsd-questions@FreeBSD.ORG Wed Apr 9 19:41:43 2014 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id D912F971 for ; Wed, 9 Apr 2014 19:41:43 +0000 (UTC) Received: from mx1.fisglobal.com (mx1.fisglobal.com [199.200.24.190]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "mx1.fisglobal.com", Issuer "VeriSign Class 3 Secure Server CA - G3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 479E61653 for ; Wed, 9 Apr 2014 19:41:43 +0000 (UTC) Received: from smarthost.fisglobal.com ([10.132.206.192]) by ltcfislmsgpa04.fnfis.com (8.14.5/8.14.5) with ESMTP id s39JfXm6001458 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Wed, 9 Apr 2014 14:41:33 -0500 Received: from THEMADHATTER (10.242.181.54) by smarthost.fisglobal.com (10.132.206.192) with Microsoft SMTP Server id 14.3.174.1; Wed, 9 Apr 2014 14:41:32 -0500 From: Sender: Devin Teske To: "'Gary Aitken'" , "'FreeBSD Mailing List'" References: <53459F47.6080407@dreamchaser.org> In-Reply-To: <53459F47.6080407@dreamchaser.org> Subject: RE: blindly passing on shell flags ($- )? Date: Wed, 9 Apr 2014 12:41:30 -0700 Message-ID: <0f3901cf542b$b58e4300$20aac900$@FreeBSD.org> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 15.0 Thread-Index: AQIcYplX4QdT/SsGFA+8+RHRmCA6VppvkE8g Content-Language: en-us X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.11.96, 1.0.14, 0.0.0000 definitions=2014-04-09_04:2014-04-09,2014-04-09,1970-01-01 signatures=0 X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Apr 2014 19:41:44 -0000 > -----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.