Help with write a master script to automatic 1000 process

Hi,

I just random create 1000 folder which range from 0-20,000 . Inside all the folder, I have copy over the same program named as run.sh to run in each folder.

Can I know that how to write a master script to let it automatic run the "run.sh" in each different random folder create?

Some random folder create :

500/
18805/
641/
.
.
.

If just run the "run.sh" in one folder, I will use the following command :

sh run.sh > run_log

But it a bit tedious if I have 1000 different folder need automatic run the same program.

I was thinking do use "for ... do ... done ...", but I don't have much idea so far :confused:

Why don't you, inside the loop, cd into the directory represented by the loop variable, sh run.sh > run_log , and then cd back to the parent dir, ready for the next loop?

Unless we are missing something here your for .. do .. done loop will be like this:

for folder in [0-9]*
do
   if [ -d "$folder" ]
   then
      cd "$folder"
      sh run.sh > run_log
      cd ..
   fi
done

Why copy run.sh to each folder?
You should be able to load the script from any path e.g. sh ../run.sh > run_log or sh ~/bin/run.sh > run_log .
Is each slightly different in some way?

Hi RudiC,

I have thinking to do something like :

for f in 500/ 18805/ 641/ ......
do
cd $f
sh run.sh > run_log
cd ..
done

I just not sure is there any smarter way to edit the "for f in 500/ 18805/ 641/ ......
" instead of I list out all the 1000 folder name in first line?

Thanks for any advice.

---------- Post updated at 08:15 PM ---------- Previous update was at 08:13 PM ----------

Hi Chubler_XL,

sh run.sh > run_log

can only run and work in each folder separately.
It can't run at any other path. That was because the input file in each folder is slightly different. But they all must use "run.sh" to extract out the result.

I will try out your script.
Hope it works :slight_smile:

Where the script is loaded from has no bearing on what directory it runs in, if you change to directory ./1008 and run /usr/local/bin/run.sh it will still execute in the 1008 folder even though the script is loaded from another folder. You will save yourself a lot of grief later if you keep 1 version of the script as you won't have to update 1000 different scripts for every change, try putting run.sh in a bin/ directory under your HOME and then run:

for folder in [0-9]*
do
   if [ -d "$folder" ]
   then
      cd "$folder"
      sh $HOME/bin/run.sh > run_log
      cd ..
   fi
done

I think you will be pleasantly surprised.

Hi Chubler_XL,

Your idea is awesome :slight_smile:
I'm trying out both of your advice.
It works fine so far :slight_smile:

It is still running at the moment.

Thanks a lot and again.