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