'run zsh command for all files in a folder

I have a models folder that contains multiple sql files for example models/mart/table1.sql, models/mart/table2.sql, models/mart/table3.sql

I run this command manually on the terminal:

dbt run-operation generate_model_yaml --args '{"model_name": "table1"}'

However, instead of running it individually for each table, I want to include it in the Bitbucket pipeline. How can I modify the command such that it runs in a loop? It should be able to extract the tablename (filename) for all files from a specified folder eg (models/mart) and then run the command accordingly by replacing the model_name by the filename each time?

pipelines:
  custom: 
    dbt-run-default:
          - step:
              name: 'Compile'
              image: fishtownanalytics/dbt:1.0.0
              script:
                - cd dbt_4flow
                - dbt deps --profiles-dir ./profiles


Solution 1:[1]

I'm not sure I quite understand which parts of the paths you want to pick apart. Does this work?

for file in models/mart/*.sql; do
    table=$(basename "$file" .sql)
    model=${file%/*}
    printf " --args '{\"%s\": \"%s\"}'\n" "$model" "$table"
    dbt run-operation generate_model_yaml --args "{\"model_name\": \"$table\"}"
done
  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1