STiGYFishh
STiGYFishh

Reputation: 788

Wait for .sh script to finish before executing another .sh script?

Basically I want to run two shell scripts but I want one of the scripts to run only after the other script has finished how would I go about doing this? I've read a bit about Until and While but I don't quite understand how I would use them. I'm sure this is very simple however I am very new to Linux.

Upvotes: 4

Views: 13331

Answers (2)

jaybee
jaybee

Reputation: 955

Simply use the && connector (i.e. a compound-command):

./script1.sh && ./script2.sh

But please consider that in this case the second script will be executed only if the first returns a 0 exit code (i.e. no error). If you want to execute the sequence whatever the result of the first script, simply go:

./script1.sh ; ./script2.sh

You can test the behaviour of the two connectors:

$ true && echo aa
aa
$ false && echo aa
$ false ; echo aa
aa

Upvotes: 6

bachN
bachN

Reputation: 612

Let me give you an example :

ScriptA = print msg "I am A" and after user input it sleeps for 5 sec

ScriptB = print msg "I am B" and wait for user input

scriptC = different calls to ScriptA and ScriptB

ScriptA :

 #!/bin/bash
 read -p "I am A"
 sleep 5
 exit

ScriptB :

 #!/bin/bash
 read -p "I am B"
 exit

ScriptC :

 #!/bin/bash
 ./ScriptA
 ./ScriptB
 exit

And now lets run ScriptC.sh :

 [prompt] $ ./ScriptC.sh
 I am A               (enter)
 //wait for 5         
 I am B               (enter)

So ScriptC wait each command to finish before executing the next command, if you dont want to wait the first script you can add (&) after command ( ./ScriptA.sh & ). In this case ScriptC will execute ScriptB immedietly after hitting "Enter" and it will not wait 5 Sec.

I hope it helps

Upvotes: 1

Related Questions