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.
find
result of all you need is select files from that which match the pattern. Use the pattern directly instead offind
to get the list of files. – Marcus Müller Feb 25 '23 at 08:01find
command. – Saeed Neamati Feb 25 '23 at 08:04==
works for static values, but not for dynamic variables? – Saeed Neamati Feb 25 '23 at 08:07AdminApi == *Api
. So, technically it's not anequality operator
. But thank you, I'll check those operators. – Saeed Neamati Feb 25 '23 at 08:18bash -x
reveal why it doesn't work? – Stéphane Chazelas Feb 25 '23 at 10:01{
or}
inside command substitution. Sure, it works, but only because almost any shell syntax will work inside$()
- it's not necessary and it adds nothing of any value here. In fact, your group command shouldn't even work because the list of commands need to be terminated by either a newline or a semi-colon (seeman bash
, search for the section headed "Compound Commands"). i.e. there should be a;
between thesort
and the}
. – cas Feb 25 '23 at 10:01bash -x
saved me. Thank you. It was because of a stupid typo in my code. But I learned how to debug bash at least. – Saeed Neamati Feb 25 '23 at 10:51BASH_XTRACEFD=7 7> file.log bash -x ./the-script
and look for relevant entries infile.log
. Or just add(set - o xtrace; ...)
wrapping around the parts you want to trace. – Stéphane Chazelas Feb 25 '23 at 10:51