Using zsh instead of bash would make this a lot easier (and reliable):
typeset -A patterns=(
'*Api' .
'*Panel' .
'Common' .
'Site*' .
'.*' .
)
for dir in /some/path/*(ND/:t); do
matched_patterns=( ${(k)patterns[(K)$dir]} )
if (( $#matched_patterns )) print -r -- $dir matched ${(j[, ])matched_patterns}
done
Gives for instance:
.Panel matched .*, *Panel
SiteApi matched Site*, *Api
That uses the K subscript flag which for associative arrays causes the expansion to return the elements for which the key as a pattern matches the subscript. And with the k parameter expansion flag, it's the key as opposed to the value that is returned. You could remove it and define the associative array as:
typeset -A patterns=(
'*Api' 'API pattern'
'*Panel' 'panel pattern'
Common Common
'Site*' 'site pattern'
'.*' 'hidden file'
)
To get:
.Panel matched hidden file, panel pattern
SiteApi matched site pattern, API pattern
For instance.
If the aim is just to get the list of directories that match either of these patterns, then, it's just:
patterns=( '*Api' '*Panel' Common 'Site*' '.*' )
dirnames=( /some/path/(${(j[|])~patterns})(ND/:t) )
print -rC1 -- $dirnames
Or for those that match none of them:
set -o extendedglob
patterns=( '*Api' '*Panel' Common 'Site*' '.*' )
dirnames=( /some/path/^(${(j[|])~patterns})(ND/:t) )
print -rC1 -- $dirnames
As for your approach, you may want to read:
Though none of those would explain why it doesn't work for you.
It works for me where I see:
Checking .Panel *Api ...
Checking .Panel *Panel ...
Matched .Panel *Panel
Checking .Panel Common ...
Checking .Panel Site*, ...
Checking .Panel .* ...
Matched .Panel .*
Checking SiteApi *Api ...
Matched SiteApi *Api
Checking SiteApi *Panel ...
Checking SiteApi Common ...
Checking SiteApi Site* ...
Matched SiteApi Site*
Checking SiteApi .* ...
You may want to run your script with bash -o xtrace (same as bash -x) to see what's going on.
Or:
BASH_XTRACEFD=7 7> file.log bash -o xtrace ./the-script
To save the tracing output in a file.
Or add some set -o xtrace (+o to disable) in chosen places to enable/disable that tracing.