3

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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
STiGYFishh
  • 708
  • 3
  • 8
  • 16
  • I don't think this is going to help in my case. I want one shell script to start another and then wait for it to finish and start it again. – STiGYFishh Aug 08 '14 at 11:42
  • Then if this is what you want, it's quite different: see [there](http://stackoverflow.com/questions/1821968/pid-of-last-started-process-in-bash-script). The waiting mechanism is brought by: `./script2 & ; some_pid=$! ; wait $some_pid` that you will write into your script1, possibly in a loop, etc. – jaybee Aug 08 '14 at 12:11
  • @jaybee, `& ;` is a syntax error. Just `script2 & some_pid=$!`... but I'm unclear on what that's buying here when you just immediately call wait; might as well have not backgrounded it at all. – Charles Duffy Aug 08 '14 at 15:07
  • @STiGYFishh, if your first script just runs `./other_script; other_stuff`, then it *does* wait for the `other_script` to finish before proceeding to `other_stuff`, automatically, unless that other program is specifically and intentionally doing something to detach its parent. If you're trying to wait for something to finish when it's actively working to evade letting its parent know when it's finished, that's a different question from the usual case. – Charles Duffy Aug 08 '14 at 15:08
  • So, back to the original question -- @STiGYFishh, why do you think this behavior isn't what you already are getting out-of-the-box? (This might require that you be specific about what software you're running and how it's behaving, since the unusual cases where something doesn't do what you're asking for are, just that, unusual). – Charles Duffy Aug 08 '14 at 15:12
  • @CharlesDuffy, thanks for pointing out the issue with `& ;`, I had the idea of writing the snippet on three lines but within a comment, I just thought of replacing the newlines by semicols... At the end of the day your point is right, the OP should clarify what is needed. – jaybee Aug 08 '14 at 19:28

2 Answers2

5

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
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
jaybee
  • 925
  • 4
  • 11
  • The question didn't say anything about running the second script only if the first succeeds. The `./script1.sh ; ./script2.sh` should probably be mentioned first, with `&&` as an afterthought. And you don't need a semicolon; just put the two commands on separate lines. – Keith Thompson Aug 08 '14 at 15:46
1

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

bachN
  • 592
  • 3
  • 14