From owner-freebsd-questions@FreeBSD.ORG Sat Jun 5 23:20:51 2010 Return-Path: Delivered-To: questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id F06B5106564A for ; Sat, 5 Jun 2010 23:20:51 +0000 (UTC) (envelope-from aiza21@comclark.com) Received: from avmxsmtp2.comclark.com (avmxsmtp2.comclark.com [202.69.191.116]) by mx1.freebsd.org (Postfix) with ESMTP id 09D188FC12 for ; Sat, 5 Jun 2010 23:20:50 +0000 (UTC) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArwZAPd4CkzKRa1aPGdsb2JhbAAHh2iWcQEBAQE1vX6FFwSDSA X-IronPort-AV: E=Sophos;i="4.53,369,1272816000"; d="scan'208";a="2173081" Received: from unknown (HELO [10.0.10.3]) ([202.69.173.90]) by avmxsmtp2.comclark.com with ESMTP; 06 Jun 2010 07:20:47 +0800 Message-ID: <4C0ADBCF.8040506@comclark.com> Date: Sun, 06 Jun 2010 07:20:47 +0800 From: Aiza User-Agent: Thunderbird 2.0.0.17 (Windows/20080914) MIME-Version: 1.0 To: Robert Bonomi , "questions@freebsd.org" References: <201006051513.o55FDCKj020952@mail.r-bonomi.com> In-Reply-To: <201006051513.o55FDCKj020952@mail.r-bonomi.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: Subject: Re: .sh & getopts X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 05 Jun 2010 23:20:52 -0000 Robert Bonomi wrote: >> Date: Sat, 05 Jun 2010 20:51:28 +0800 >> From: Aiza >> To: Robert Bonomi >> Subject: Re: .sh & getopts >> >> Robert Bonomi wrote: >>> >>>> From owner-freebsd-questions@freebsd.org Thu Jun 3 23:36:28 2010 >>>> Date: Fri, 04 Jun 2010 12:35:56 +0800 >>>> From: Aiza >>>> To: "questions@freebsd.org" >>>> Cc: >>>> Subject: .sh & getopts >>>> >>>> Have this code >>>> >>>> shift; while getopts :ugr: arg; do case ${arg} in >>>> u) action="freebsd-update";; >>>> g) action="freebsd-upgrade";; >>>> r) action="freebsd-rollback";; >>>> ?) exerr ${cmd_usage};; >>>> esac; done; shift $(( ${OPTION} -1 )) >>>> >>>> >>>> Command being executed looks like this, cmd action -flags aaaa bbbb >>>> >>>> Only a single -flag in allowed on the command. >>>> >>>> $# gives a count of parms ie: aaaa bbbb. in this example a count of 2. >>>> >>>> I am looking for something to check that holds the number of flags on >>>> the command. so I can code. if flag_count gt 1 = error >>>> >>>> Is there such a thing created by getopts? >>> Why bother?? >>> >>> flag_count=0 >>> shift; while getopts :ugr: arg >>> if flag_count = 1; then >>> exerr ${cmd_usage} >>> fi >>> flag_count=1; >>> do case ${arg} in >>> {{blah-blah}} >>> >> nope dont work. > > Yup. I was in a hurry, got the code mechanics wrong. it needs to be: > > flag_count=0 > shift; > while getopts :ugr: arg ; do > if flag_count = 1; then > exerr ${cmd_usage} > fi > flag_count=1; > case ${arg} in > {{blah-blah}} > ecas > done > > > I think I see what your are saying. so to adapt it to my code flag_count=0 shift; while getopts :ugr: arg; do flag_count + 1; case ${arg} in u) action="freebsd-update";; g) action="freebsd-upgrade";; r) action="freebsd-rollback";; ?) exerr ${cmd_usage};; esac; done; shift $(( ${OPTION} -1 )) if flag_count gt 3; then exerr ${cmd_usage} fi I think I got the concept correct, but the flag_count + 1 is not correct. I get "flag_count: not found" when I run it this way.