rohit
rohit

Reputation: 119

Run multiple shell scripts in a single script

I have 50 shell scripts: proc_0.sh, proc_500.sh, proc_1000.sh...proc_25000.sh.

Each of them perform same operation but on different files.

I would like to execute all these shell scripts in a single master shell script, one after another.

Here is what I have tried so far:

#!/bin/bash/

cd $(pwd)

i=0

for i in {0..25000..500}
do
     chmod +x proc_${i}.sh
     bash proc_${i}.sh
done

exit

Now, I am not sure about this but it seems that these scripts are running in parallel or getting mixed up in some way. I say this because when I run a few of these scripts individually, they give correct results but not when run in the way mentioned above. It could also be that there might be a problem with the program these scripts are executing.

So, could anyone please tell me if this is the right way to run multiple shell scripts one after another? It would help me narrow down the problem.

Thanks

Upvotes: 0

Views: 21203

Answers (1)

thom
thom

Reputation: 2332

No, it is a bit messy how you are doing it.

#!/bin/bash/

Drop the last slash or it won't work

cd $(pwd)

this is the same as cd . .cd to the current directory is bogus. Delete this from your script.

i=0

You can delete this also: It is followed by a for loop which already initializes $i

 chmod +x proc_${i}.sh

You are explicitly setting the execute flag EVERY time this script runs. It does no harm but once is enough.

bash proc_${i}.sh

You don't need to use the "bash" statement. Regardless of this, it will not run because you forgot the path to your command. Unless you are running this from a directory like /usr/local/bin: Always use a path !

And no, your scripts are not running in parallel. You forgot to background them.

try this instead:

#!/bin/bash

for i in {0..25000..500}
do
     /path/to/proc_${i}.sh &   #<- don't forget to replace "/path/to" with the
done                           #   real directory where the proc_ files reside

wait

This will get your computer flooded with your 50 processes at once.

run top to see them running.

It would be best if you had all these proc scripts together in one subdirectory exclusively and then run:

#!/bin/bash

for i in "/path/to/procscripts/"*
do
    "$i" &
done

wait

This would make it more flexible and more easy on yourself.

Also making all your scripts executable could then be easier:

chmod a+x "/path/to/procscripts/"*

Upvotes: 5

Related Questions