0

I'm new to bash scripting and I'm trying to make a script which uses a for loop to find all the folders in the directory where it is started, and then use an if statement to find another folder within them which has a name templates, in which I then want to run a python command to execute python scripts stored in that folder.

  • My directory structure looks like this:
    all/
        this_script.sh
        one/
            templates/
                      render_j2.py
        two/
            templates/
                      render_j2.py
    
  • What I've tried for this_script-sh is
    for file in *; do
      if [[ "$file"==*"templates"* ]];
      then
        cd $file
      fi
    done
    
    and also tried this:
    for file in *; do
    find . templates
    python render_j2.py; 
    done
    

My main issue is that I can't enter the templates folder, and even when I can, the python command cant find the python files to run.

3 Answers 3

3

Since you are already using find, why not offload the entire task to it? You could try the following:

find . -type f -path "*/templates/render_j2.py" -execdir python render_j2.py \;

This will search for all files render_j2.py in a directory templates below your starting point, and in the directory where the file was found, execute your python command.

2

There's a number of things wrong with this:

  • Neither version is testing if $file is a directory
  • When the first version uses cd $file then $file will not be a valid path in the next iteration of the loop, because you changed directories; putting cd in a subshell (using parentheses) along with things you want to do in that directory would fix this.
  • In the second version, the find syntax is wrong to do what you want
  • it doesn't make sense to run find in a loop like this, find does its own loop internally

Something like this might work:

find . -name templates -type d -execdir python {}/render_j2.py \;

This would be equivalent to

(cd all/one && python ./templates/render_j2.py )
(cd all/two && python ./templates/render_j2.py )
...

which still might not be exactly what you want.

-1

This is kind of a non-answer but this seems like an over complicated setup. If you have an unstated reason you must or just want (to learn perhaps) the loop then this isn't going to be your answer but...You can do this sans loop with the find command.

Something like

find $HOME/my/folders -type d -name templates -exec my.py {} \;
2
  • Almost! It looks like the python script needs to be run from within the directory where the render_j2.py is located, that's whythe other answers used -execdir.
    – terdon
    Jun 7, 2022 at 12:12
  • These three answers were typed pretty much simultaneously. I almost included --exec instead of -execdir
    – user10489
    Jun 8, 2022 at 4:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .