4

I have 4 shell scripts and I want to run them in a sequencing way:

script1 -> script2 -> script3 ->script4

in a local machine can I ensure this by make the scripts executable and creating a new shell script like this:

#!/bin/bash
. script1.sh
. script2.sh
. script3.sh
. script4.sh

and if one of the scripts is in a remote machine, (for example script2.sh) how can I:

  1. run the shell script remotely
  2. ensure the sequencing.

Note : all of the scripts have an infinite loop.

2 Answers 2

4

I assume you can run the script with: ./script1.sh (which start the script in a shell) instead of . script1.sh (which will source them in your current shell) ?

If so:

#!/bin/bash

./script1.sh   &&
{ ssh user@remote "cd /path/to/  && script2.sh" ; }  &&
./script3.sh   &&
./script4.sh

&& ensure that the following instructions is executed ONLY if the previous returned "0" (="ok") (so make sure your script does return the proper "0" if OK, and something else (a small positive integer, such as "1") if NOT OK).

(Note that you can have && at the end of a line, as I did here, and the following thing on the next line, without the need to \ the newline in-between. "&&" at the end of the line tells bash that the line can be continued on the following line. [thank to @Dennis for correcting me: I thought it also worked to not put a '\newline' when "&&" was on the next line, which is more readable... but doesn't work. If you want "&&" on the next line, you need to backslash the previous newline])

6
  • and what if the script1 has an infinite while(I want it to be like that) how can I get the return value "0" or "1" @Olivier Dulac Commented May 21, 2014 at 8:47
  • if it is an infinite loop, then you don't want to wait for it to finish before doing script2 and the others? if so : ./script1.sh & is the first line. and the second is ssh ...... (no "&&" at the beginning of the 2nd line) Commented May 21, 2014 at 8:56
  • but what if all the scripts have an infinite loop @Olivier Dulac Commented May 21, 2014 at 8:58
  • 2
    Then you need to rephrase your question where you say, I quote script1 -> script2 -> script3 ->script4 and ensure the sequencing (you don't want to wait infinitely after each script...). Just not use "&&" at all, if you just want to start them in this order. Commented May 21, 2014 at 9:00
  • Are you positive about not needing \ for line continuation? If I try to do that in bash it complains about a syntax error ("unexpected token &&"). Commented Oct 26, 2015 at 14:55
1

If you can ssh to the remote machine and the scripts are already on the remote machine in subdirectory test of your account there you can just do:

#!/bin/bash
. script1.sh
ssh your_name@remote_machine "cd test; . script2.sh"
. script3.sh
. script4.sh

the script will wait for the remote command to return before sourcing script3.sh

If the script is not there yet, or changes regularly, you can use scp to copy it to the known location first (assuming you have made test once):

#!/bin/bash
. script1.sh
scp script2.sh your_name@remote_machine:~/test/
ssh your_name@remote_machine "cd test; . script2.sh"
. script3.sh
. script4.sh

And if script2.sh creates some output in a file, you can of course copy that back after the ssh has finished.

2
  • it works but he asks for the password, how can I include the password in ssh command without put it mannually @Anthon Commented May 21, 2014 at 8:38
  • 2
    create a public/private key pair (ssh-keygen) and copy the private key over to the remote machine (ssh-copy-id) Commented May 21, 2014 at 8:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.