Reqwy
Reqwy

Reputation: 13

Run sh scripts successively

I'd like to write .sh script that runs several scripts in the same directory one-by-one without running them concurrently (e.x. while the first one is still executing, the second one doesn't start executing).

Could you tell me the command, that could be written in front of script's name that does the actual thing?

I've tried source but it gives the following message for every listed script

./outer_script.sh: source: not found

Upvotes: 0

Views: 132

Answers (3)

diidu
diidu

Reputation: 2863

That is normal behavior of the bash script. i.e. if you have three scripts:

script1.sh:

echo "starting"
./script2.sh
./script3.sh
echo "done"

script2.sh:

while [ 1 ]; do
    echo "script2"
    sleep 2
done

and script3.sh:

echo "script3"

The output is:

starting
script2
script2
script2
...

and script3.sh will never be executed, unless you modify script1.sh to be:

echo "starting"
./script2.sh &
./script3.sh &
echo "done"

in which case the output will be something like:

starting
done
script2
script3
script2
script2
...

So in this case I assume your second level scripts contain something that starts new processes.

Upvotes: 0

Müller
Müller

Reputation: 1023

Have you included the line #!bin/bash in your outer_script? Some OS's don't consider it to be bash by default and source is bash command. Else just call the scripts using ./path/to/script.sh inside the outer_script

Upvotes: 0

Score_Under
Score_Under

Reputation: 1216

source is a non-standard extension introduced by bash. POSIX specifies that you must use the . command. Other than the name, they are identical.

However, you probably don't want to source, because that is only supposed to be used when you need the script to be able to change the state of the script calling it. It is like a #include or import statement in other languages.

You would usually want to just run the script directly as a command, i.e. do not prefix it with source nor with any other command.

As a quick example of not using source:

for script in scripts/*; do
    "$script"
done

If the above does not work, ensure that you've set the executable bit (chmod a+x) on the necessary scripts.

Upvotes: 1

Related Questions