I am writing a BASH script. I have a list of files and I would like to make sure that each is of a specific pattern (ie [AT]*.L2). If not I would like to remove that file. How do I test whether a filename matches a given pattern?
Thanks a lot.
Yes I was thinking about doing that. However, the pattern that I would like to match is condition dependent. I would like to do something like this:
if [ $START_LEVEL = 0 ]; then
PATTERN_ARRAY=('[AT]*.L0_LAC' 'MOD00.[AP]*.PDS')
elif [ $START_LEVEL = 1 ]; then
PATTERN_ARRAY=([AST]*.L1A_[GL]AC* S*.L1A_MLAC*)
elif [ $START_LEVEL = 2 ]; then
PATTERN_ARRAY=([AST]*.L2_[GL]AC* S*.L2_MLAC*)
elif [ $START_LEVEL = 3 ]; then
PATTERN_ARRAY=([AST]*_*_*.L3)
fi
for ENTRY in *; do
for PATTERN in ${PATTERN_ARRAY[@]}; do
case $ENTRY in
$PATTERN)
echo 'Entry matches pattern'
;;
*)
echo 'Entry does NOT match pattern'
;;
esac
done
done
However, the shell appears to be performing the pattern matching in if statements. Is there a way to get around that? Thank you.