Try the following Regular Expression with
egrep (or
grep -E) :
^(.*,)?hal(,.*)$
This RE recognizes the following strings (_ is any char):
___,hal,___
hal,___
___,hal
hal
Code:
if echo "$STRING" | egrep -q '^(.*,)?hal(,.*)?$' ; then
echo OK
else
echo ZONK
fi
With
ksh you can use the
= operator instead of grep, with the pattern :
?(*,)hal?(,*)
Code:
#!/usr/bin/ksh
if [[ "$STRING" = ?(*,)hal?(,*) ]] ; then
echo OK
else
echo ZONK
fi
If you really want to recognize only the first form (___,hal,___), modify your RE by
',hal,'
Code:
#!/bin/sh
STRING="hal,kohal,obahalta,hal9000"
if [ `echo $STRING | grep ',hal,` ]; then
echo OK
else
echo ZONK
fi