0

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

6
  • {0..25000..500} : is it {0..500..25000} that you wanted ? what about seq 0 25000 500 ? Commented Dec 3, 2013 at 17:33
  • I wanted to go from 0 to 25000 with an interval of 500. This corresponds to the filenames. What would seq 0 25000 500 do exactly? Thanks. Commented Dec 3, 2013 at 17:36
  • try seq in a shell script Commented Dec 3, 2013 at 17:38
  • no, the {0..25000..500} bit works according to specs and is the best choice in this case. Do not use seq if you don't need it. It is not a build-in, it is slower, eats more resources and it is depricated. Commented Dec 3, 2013 at 17:42
  • @thom, who has deprecated seq? Commented Dec 3, 2013 at 18:27

1 Answer 1

5

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/"*
Sign up to request clarification or add additional context in comments.

1 Comment

why wait is used here.. what will happen if i don't use wait here ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.