From: WH on
Dear all,

In bash, we can use 'case' statement so do some simple string matching.
For example

read quit
case "$quit" in
Y*|y*|"")
exit 1
;;
*)
;;
esac

If we don't want to use case (since the condition is simple), we can use
'if' and 'grep'

read quit
if echo $quit | grep -vq '^[^Yy]'; then
exit 1
fi

I am wondering if there are some bash buildin functions other than
'case' to do this simple matching?


Thanks,
WH
From: pk on
WH wrote:

> Dear all,
>
> In bash, we can use 'case' statement so do some simple string matching.
> For example
>
> read quit
> case "$quit" in
> Y*|y*|"")
> exit 1
> ;;
> *)
> ;;
> esac
>
> If we don't want to use case (since the condition is simple), we can use
> 'if' and 'grep'
>
> read quit
> if echo $quit | grep -vq '^[^Yy]'; then
> exit 1
> fi
>
> I am wondering if there are some bash buildin functions other than
> 'case' to do this simple matching?

Yes, bash has a regex match operator that can be used within the special [[
... ]] test construct. It supports matching against EREs (ie, similar to
"grep -E"):

if [[ "$quit" =~ ^[Yy] ]]; then ...; fi

it's important to NOT quote the regular expression, otherwise bash treats it
as a literal string.
From: Chris F.A. Johnson on
On 2010-05-14, WH wrote:
> Dear all,
>
> In bash, we can use 'case' statement so do some simple string matching.
> For example
>
> read quit
> case "$quit" in
> Y*|y*|"")
> exit 1
> ;;
> *)
> ;;
> esac
>
> If we don't want to use case (since the condition is simple),

Why would you not want to use case? It *is* simple, and it's
portable.


--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
===== My code in this post, if any, assumes the POSIX locale =====
===== and is released under the GNU General Public Licence =====
From: Stephane CHAZELAS on
2010-05-15, 05:35(+00), Chris F.A. Johnson:
> On 2010-05-14, WH wrote:
[...]
>> In bash, we can use 'case' statement so do some simple string matching.
>> For example
>>
>> read quit
>> case "$quit" in
>> Y*|y*|"")
>> exit 1
>> ;;
>> *)
>> ;;
>> esac
>>
>> If we don't want to use case (since the condition is simple),
>
> Why would you not want to use case? It *is* simple, and it's
> portable.

Seconded.

Another portable though not as legible one:

[ "${quit#[yY]}" ] || exit

--
Stéphane
From: Thomas 'PointedEars' Lahn on
Stephane CHAZELAS wrote:

> Chris F.A. Johnson:
>> On 2010-05-14, WH wrote:
> [...]
>>> In bash, we can use 'case' statement so do some simple string matching.
>>> For example
>>>
>>> read quit
>>> case "$quit" in
>>> Y*|y*|"")
>>> exit 1
>>> ;;
>>> *)
>>> ;;
>>> esac
>>>
>>> If we don't want to use case (since the condition is simple),
>>
>> Why would you not want to use case? It *is* simple, and it's
>> portable.
>
> Seconded.
>
> Another portable though not as legible one:
>
> [ "${quit#[yY]}" ] || exit

When I suggested that in de.comp.os.unix.shell, it has been brought to my
attention that by contrast it is unfortunately _not_ portable (at least not
as portable as `case') even though SUSv3 specifies it. I have been told
that this feature was only introduced with the first ksh versions (which
explains why it is in SUSv3, and bash), and I have subsequently found out by
reading the original sh documentation that it is not Bourne-shell
compatible.

See [de] <news:4ba910e9$0$14077$4d3ebbfe(a)news1.pop-hannover.net> and
followups for more.


PointedEars