Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 5 Jun 2010 20:30:14 -0500
From:      Brandon Gooch <jamesbrandongooch@gmail.com>
To:        Aiza <aiza21@comclark.com>
Cc:        "questions@freebsd.org" <questions@freebsd.org>, Robert Bonomi <bonomi@mail.r-bonomi.com>
Subject:   Re: .sh & getopts
Message-ID:  <AANLkTimDVOQE-SrT7YO4UD3tktD81jy8px6dbbB83-Dl@mail.gmail.com>
In-Reply-To: <4C0ADBCF.8040506@comclark.com>
References:  <201006051513.o55FDCKj020952@mail.r-bonomi.com> <4C0ADBCF.8040506@comclark.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sat, Jun 5, 2010 at 6:20 PM, Aiza <aiza21@comclark.com> wrote:
> 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 =A0Thu Jun =A03 23:36:28 201=
0
>>>>> 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
>>>>> =A0 =A0u) action=3D"freebsd-update";;
>>>>> =A0 =A0g) action=3D"freebsd-upgrade";;
>>>>> =A0 =A0r) action=3D"freebsd-rollback";;
>>>>> =A0 =A0?) 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 =3D error
>>>>>
>>>>> Is there such a thing created by getopts?
>>>>
>>>> Why bother??
>>>>
>>>> =A0flag_count=3D0
>>>> =A0shift; while getopts :ugr: arg
>>>> =A0 if flag_count =3D 1; then
>>>> =A0 =A0 exerr ${cmd_usage}
>>>> =A0 fi =A0 =A0flag_count=3D1;
>>>> =A0 do case ${arg} in
>>>> =A0 {{blah-blah}}
>>>>
>>> nope dont work.
>>
>> Yup. =A0I was in a hurry, got the code mechanics wrong. =A0it needs to b=
e:
>> =A0 =A0 flag_count=3D0
>> =A0 =A0 shift; =A0 =A0 =A0while getopts :ugr: arg ; do
>> =A0 =A0 =A0 if flag_count =3D 1; then
>> =A0 =A0 =A0 =A0 exerr ${cmd_usage}
>> =A0 =A0 =A0 fi =A0 =A0 =A0 =A0flag_count=3D1;
>> =A0 =A0 =A0 case ${arg} in
>> =A0 =A0 =A0 =A0 {{blah-blah}}
>> =A0 =A0 =A0 ecas
>> =A0 =A0 done
>>
>>
>>
> I think I see what your are saying. so to adapt it to my code
>
>
> flag_count=3D0
> shift; while getopts :ugr: arg; do
> =A0flag_count + 1;
> =A0case ${arg} in
> =A0 =A0 u) action=3D"freebsd-update";;
> =A0 =A0 g) action=3D"freebsd-upgrade";;
> =A0 =A0 r) action=3D"freebsd-rollback";;
> =A0 =A0 ?) exerr ${cmd_usage};;
> =A0esac; done; shift $(( ${OPTION} -1 ))
>
>
> =A0if flag_count gt 3; then
> =A0 =A0exerr ${cmd_usage}
> =A0fi
>
>
> 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.

You could use:

flag_count=3D`expr $flag_count + 1`

or...

...anyone else?

These types of open-ended questions are always fun :)

-Brandon



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?AANLkTimDVOQE-SrT7YO4UD3tktD81jy8px6dbbB83-Dl>