From: Rakesh Sharma on 4 Apr 2010 05:37 On Apr 4, 7:14 am, Ben Bacarisse <ben.use...(a)bsb.me.uk> wrote: > Rakesh Sharma <sharma...(a)hotmail.com> writes: > > I want to match a string of numbers followed optionally by either a + > > or a -. > > The corner cases are: the number cannot begin with a 0 unless it's a > > solitary 0. > > > So expressing this in terms of regular expressions I can write: > > > [0][+-]? | [1-9][0-9]*[+-]? > > [0] is little odd! You can make the ending more clear by re-writing > this as: > > (0|[1-9][0-9]*)[+-]? > > > So far so good, but how would I write the equivalent functionality in > > terms > > of shell file patterns of the kind found in the 'case'..'esac' > > statments of bourne shell? > > bash can but not a POSIX shell. You can get close if can limit the > number of digits by simply writing out all the cases, but you can't > even use [0-9] (you need [0123456789] instead) so the end result will > be horrid and error prone. > > -- > Ben. I came up with this, but not sure if it'll work: (yet to test it) case $1 in '' | *[!0-9+-]* | [+-] | *[+-]?* | [0][0-9]* ) echo >&2 "Incorrect arg: '$1'"; exit 1;; *) :;; esac -- Rakesh
From: Janis Papanagnou on 4 Apr 2010 07:59 Rakesh Sharma schrieb: > Hi, > > I want to match a string of numbers followed optionally by either a + > or a -. > The corner cases are: the number cannot begin with a 0 unless it's a > solitary 0. > > So expressing this in terms of regular expressions I can write: > > [0][+-]? | [1-9][0-9]*[+-]? > > So far so good, but how would I write the equivalent functionality in > terms > of shell file patterns of the kind found in the 'case'..'esac' > statments of bourne shell? In addition to the already given answers; let me just point to the expr(1) command that allows to check variables against BRE regexps and you can conditionally branch according to its exit status. BTW; are you really *required* to use a bourne shell? There are so simple solutions in all modern shells. Janis > > Thanks > > > -- Rakesh
From: Ben Bacarisse on 4 Apr 2010 09:49 Rakesh Sharma <sharma__r(a)hotmail.com> writes: <snip> >> Rakesh Sharma <sharma...(a)hotmail.com> writes: >> > I want to match a string of numbers followed optionally by either a + >> > or a -. >> > The corner cases are: the number cannot begin with a 0 unless it's a >> > solitary 0. <snip> > I came up with this, but not sure if it'll work: (yet to test it) > > case $1 in > '' | *[!0-9+-]* | [+-] | *[+-]?* | [0][0-9]* ) > echo >&2 "Incorrect arg: '$1'"; exit 1;; > *) :;; > esac That looks right though it's not what you asked for! -- Ben.
From: Rakesh Sharma on 4 Apr 2010 10:32 On Apr 4, 6:49 pm, Ben Bacarisse <ben.use...(a)bsb.me.uk> wrote: > Rakesh Sharma <sharma...(a)hotmail.com> writes: > > <snip> > > >> Rakesh Sharma <sharma...(a)hotmail.com> writes: > >> > I want to match a string of numbers followed optionally by either a + > >> > or a -. > >> > The corner cases are: the number cannot begin with a 0 unless it's a > >> > solitary 0. > <snip> > > I came up with this, but not sure if it'll work: (yet to test it) > > > case $1 in > > '' | *[!0-9+-]* | [+-] | *[+-]?* | [0][0-9]* ) > > echo >&2 "Incorrect arg: '$1'"; exit 1;; > > *) :;; > > esac > > That looks right though it's not what you asked for! > > -- > Ben. So what you mean is that this case..esac isn't equivalent to the regular expression ( 0 | [1-9] [0-9]* ) [+-]? -- Rakesh
From: Ben Bacarisse on 4 Apr 2010 11:41 Rakesh Sharma <sharma__r(a)hotmail.com> writes: > On Apr 4, 6:49 pm, Ben Bacarisse <ben.use...(a)bsb.me.uk> wrote: >> Rakesh Sharma <sharma...(a)hotmail.com> writes: >> >> <snip> >> >> >> Rakesh Sharma <sharma...(a)hotmail.com> writes: >> >> > I want to match a string of numbers followed optionally by either a + >> >> > or a -. >> >> > The corner cases are: the number cannot begin with a 0 unless it's a >> >> > solitary 0. >> <snip> >> > I came up with this, but not sure if it'll work: (yet to test it) >> >> > case $1 in >> > '' | *[!0-9+-]* | [+-] | *[+-]?* | [0][0-9]* ) >> > echo >&2 "Incorrect arg: '$1'"; exit 1;; >> > *) :;; >> > esac >> >> That looks right though it's not what you asked for! >> >> -- >> Ben. Best not to quote sig blocks (even small ones). > So what you mean is that this case..esac isn't equivalent to the > regular expression ( 0 | [1-9] [0-9]* ) [+-]? No. I was just explaining why you got negative answers. You originally asked for a glob pattern than matched some set which was not possible without some non-standard extensions. It turns out you /can/ solve the practical problem by looking for every case that does /not/ match, but that was not what you originally asked for. Having "positive" patterns is, in general, more useful. They can be used in places other than case...esac and they make maintaining a larger set of case patterns much simpler. They are also much easier to understand. For example, it is not immediately clear what patterns fall through to the * case in your solution. -- Ben.
First
|
Prev
|
Pages: 1 2 Prev: bash: how to transfer some non-ascii code Next: very strange ksh behavior |