0

I am trying to run a script in multiple folders that calls: a software, two input files that create automatically a folder as output (in the folder of the input), like:

./soft -in1 input1 -in2 input2

Also, I have multiple folders, like this:

├── Folder1
│   ├── input1.in1
│   ├── input1.in2
│   ├── soft.py
├── Folder2
│   ├── input2.in1
│   ├── input2.in2
│   ├── soft.py                                                                             
├── script.sh

So, I want to do two processes:

First, run the script recursively (in all folders), and second, run 'X' repetitions of the script in each folde. I got this script to run recursively.

But I have problems trying to run the repetitions. I try using the seq command, but the software ./soft rewrite the output in each repetition. So, I need save the outputs in folders with the number of each repetition (e.g. \out_Folder1_rep1; \out_Folder1_rep2; \out_Folder1_rep3).

I need create the output-folders for each repetition before?. Can someone help me?

for dir in */; do
    for r in in1; do
        glob=*.${r}
        "./soft" -in1 "$dir"/$glob -in2 "$dir"/$(basename -s .tpl $glob).in2 ;
    done
done
  • 3
    Possible duplicate of [How do I write a for loop in bash](https://stackoverflow.com/q/49110/608639), [Is there a better way to run a command N times in bash?](https://stackoverflow.com/q/3737740/608639), [How do I iterate over a range of numbers defined by variables in Bash?](https://stackoverflow.com/q/169511/608639), etc. – jww Aug 30 '18 at 01:16
  • Thanks for answer, yes, I try using `seq` as proposed in these links, but the software rewrite my output-folder in each repetition. So, I need is create the output-folders for each repetition 'manually' and that the software export these outputs in their respective folders. – Sergio Bolívar Aug 30 '18 at 14:51

1 Answers1

1

First, run the script recursively (in all folders), and second, run 'X' repetitions of the script in each folde. I got this script to run recursively.

Instead of doing the recursion, first read the list of all folders in the root folder, then "change directory" to each and every entry in the list of folders and then execute your script.

Please find the sample code below:

#!/bin/sh

if [ ! -z "${1}" ];
then
    DIR_LIST=$(ls -d "${1}/*")
    for dir in "${DIR_LIST}";
    do
      cd ${dir}
      # run your script here
    done
fi
cpp_enthusiast
  • 1,239
  • 10
  • 28