Compare pattern in variable

How to check if pattern is matching in variable or not.
I want to check file name in variable with the pattern.

Eg: file_name="AB1000.csv"

Check for below patterns

pattern1 = AB???
pattern2 = abc??*

if  file_name == <patten1>
then
       ...
elif  file_name == <pattern2>
     ---
fi

Please advise. Thanks.

What system/OS/shell do you use? bash , for instance, allows for matching a variable to a regular expression.

I am using AIX... korn shell. Please advise.

DIR=/your/directory/to/search
pattern1="AB???"
ls $DIR/$pattern1 2>/dev/null || echo "No file found matching the pattern"

To perform action based on the pattern match use the below.

DIR=/your/directory/to/search
pattern1="AB???"
ls $DIR/$pattern1 1>/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
  echo "Match found"
else
  echo "No match found"
fi

I think you are searching within the variable, rather that for an actual file, so:-

You could try a case statement if you are planning lots of if-then-elif then-elif.....

file_name="AB1000.csv"
case file_name in
   AB???*)  Do the first AB action
          Do the second AB action ;;
   abc??*)  Do the first abc action
          Do the second abc action ;;
   *)    Do actions for no match ;;
esac

Of you you prefer, you can use the variable substitution commands to test if your string contains something interesting:-

file_name="AB1000.csv"

test_file_name="${file_name#AB}"

if [ "$test_file_name" != "$file_name" ]
then
   echo "Matched AB"
fi


test_file_name="${file_name#abc}"

if [ "$test_file_name" != "$file_name" ]
then
   echo "Matched abc"
fi

Does this help, or have I missed the point?

Robin
Liverpool/Blackburn
UK