Sergio Bolívar
Sergio Bolívar

Reputation: 69

A bash script that reads python files recursively and stops after the output of each file exists

I have a bash that reads *.py scripts recursively and stops when the output of each *.py files exists in the directory (a *.pkl file). The main idea of the bash is that if the output not exists, the python script has to run again until creating the output for each *.py script. 

bash.sh
model1.py
model2.py
model3.py
model1.pkl # expected output
model2.pkl # expected output
model3.pkl # expected output

However, I have a problem here: When the second/third output NOT exists (from the second/third .*py script) the bash did not run again (while if the first output NOT exists the bash run again, as should be).

My bash is the following:

#!/bin/bash

for x in $(find . -type f -name "*.py"); do
    if [[ ! -f `basename -s .py $x`.pkl ]]; then #output with the same name of *.py file
        python3 ${x}
    else
        exit 0
    fi
done

So, how I can force the bash script to run again if the output of any *.py script is missing? Or it is a problem with the name of the outputs?

I tried using the commands while read and until, but I failed to do the script read all the *.py files.

Thanks in advance!

Upvotes: 0

Views: 251

Answers (1)

change198
change198

Reputation: 2065

try this: not the best way: but at least will help you in right direction.

    keep_running(){
for f in $(find . -type f -name "*.py");
do
    file_name=$(dirname $f)/$(basename $f .py).pk1
    if [ ! -f "$file_name" ];
    then
        echo "$file_name doesn't exists" # here you can run your python script
    fi
done
}

cnt_py=0
cnt_pkl=1 

while [ $cnt_pkl -ne $cnt_py ] ; do
        keep_running
        cnt_py=`ls -1 *.py| wc -l`
        cnt_pkl=`ls -1 *.pk1| wc -l`
done

Upvotes: 1

Related Questions