Shell scripting question (Fedora 16)

Hi,

I need to write a script that runs a series of commands in multiple subfolders. The problem is that I dont want to change directory for every folder. So this is why I thought to use a master script to activate the smaller ones.

So, is there a way to command other scripts to open in subdirectories, but in sequential order, without typing in every folder you want it to open from.

I figured if I tell it to run the script eg.sh in every subfolder it would run them simultaneously right?

Sorry if its confusing, I can give an example later if no one understands where im coming from.

If the name of the script is the same in every directory, you could use the find command with the -exec option to find and execute each script.

Please do post an example. It's not too clear what you mean.

It is possible to write one single script which searches for directories in a directory tree and executes a command within each directory. The unix find command recommended above is likely to feature in the script.

Thanks heaps, an example:

Folder1/eg.sh
Folder2/eg.sh
Folder3/eg.sh
Folder4/eg.sh

So i need to run eg.sh in each folder, one at a time (I have over 100 folders so dont want to type each one individually).

for script in `find . -name eg.sh 2>/dev/null`
  do
   sh $script > $script.log 2>&1
   wait
  done

Try:

for script in */eg.sh; do
  "$script"
done

No it should run them sequentially (it is also possible to run them simultaneously if you want..)

for script will fail if sub directories have space in their name, use this instead:

 find . -name eg.sh 2>/dev/null | while read script
                                  do
                                   sh $script > $script.log 2>&1
                                   wait
                                  done

If combined with filename expansion, no.
If combined with a file list that has been word split using standard IFS after command substitution, then yes..

1 Like

Awesome, the only problem I noticed now is that the commands I want to use dont seem to work as they arnt bash commands, haha.

Im trying to run FSL commands in a script is anyone knows about FSL. Its a program for brain imaging.

using the suggested code you can generate one script that could be executed on your FSL CLI