Date: Sun, 06 Jun 2010 07:20:47 +0800 From: Aiza <aiza21@comclark.com> To: Robert Bonomi <bonomi@mail.r-bonomi.com>, "questions@freebsd.org" <questions@freebsd.org> Subject: Re: .sh & getopts Message-ID: <4C0ADBCF.8040506@comclark.com> In-Reply-To: <201006051513.o55FDCKj020952@mail.r-bonomi.com> References: <201006051513.o55FDCKj020952@mail.r-bonomi.com>
next in thread | previous in thread | raw e-mail | index | archive | help
Robert Bonomi wrote: >> Date: Sat, 05 Jun 2010 20:51:28 +0800 >> From: Aiza <aiza21@comclark.com> >> To: Robert Bonomi <bonomi@mail.r-bonomi.com> >> 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 <aiza21@comclark.com> >>>> To: "questions@freebsd.org" <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.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4C0ADBCF.8040506>