This is extended question of Howto check against zsh script's parameter in a pattern or not

While the link above solved the problem that a zsh script check agaist its first argument to list of patterns in a file separated by newline. I'd like to extend it as:

  1. Check against patterns that in multiple files in multiple folders that probably in an array of folders.

  2. Keep tracking of that matched pattern to do another task, i.e: call a .conf that setup something else.

While I ask this question, I still in multitasks and will update it later, if you have a solution, I'll much appreciated.

files=($^array_of_folders/**/*.pattern(.N)) # assuming pattern files
                                            # have a .pattern extension
pattern="($(cat -- $files | paste -sd '|' -))"
if [[ $1 = $~pattern ]]; then
   print -r -- "$1 matches $pattern"

Though if you want to know which of the patterns matched, you'd need:

patterns=(${(f)"$(cat -- $files)"})
for pattern ($patterns) {
   [[ $1 = $~pattern ]] && matching_patterns+=$pattern
print -r -- "$1 matched by $#matching_patterns patterns: $matching_patterns"
  • What would you define array_of_folders? Something like array_of_folders=(~/fder1 ~/smt/fder2 /tmp/fder3)? Also pattern is $1 - first argument of zsh script: script.zsh my_pattern
    – Tuyen Pham
    Aug 28 '18 at 15:36
  • 1
    @TuyenPham, that array_of_folders is meant to be the array of folders you refer to in your question. And yes array_of_folders=(~/fder1 ~/smt/fder2 /tmp/fder3) could be one way to define it. In your question, you say $1 is the string to match against patterns, not a pattern. Aug 28 '18 at 15:42
  • Ah yes, correct, $1 is string to match against patterns in files that inside folders.
    – Tuyen Pham
    Aug 28 '18 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.