Regular Expressions in If clause

Hi All,

I have a set of files in my directory like BED123C.txt, SED134F.txt,DEF567DF.txt. I want to execute separate scripts based on the file names.
I am using the following block of the code for this

for EachFile in `ls`
do
if [ EachFile = "B*" ]
then
sh Exe1.sh
fi
if [ EachFile = "S*" ]
then
sh Exe2.sh
fi
done

But it is not producing the desired result.
Can any one help me how to use regular expressions in if Condition in unix scripting?

Thanks in advance,

-- Raam.

But why do you want to use if? The case statement is very powerful and is made to order for just such a situation.

case $(ls) in
    B*) sh Exe1.sh  ;;
    S*) sh Exe2.sh  ;;
esac

You are missing a dollar sign in the if-statement, but I don't think it will work even then.

If you want to use if, you could experiment with the expr command or use something like this:

if [ ${EachFile:0:1} = B ]

If for some reason you do not wish to use a case statement, here is one way of doing it using if statements. This works for bash and ksh93.

for eachFile in `ls`
do
   if [[ $eachFile =~ ^B ]]
   then
      ....
   fi
   if [[ $eachFile =~ ^S ]]
   then
      ....
   fi
done