Shell – run shell scripts in a sequencing way

shellssh

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.

Best Answer

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])

Related Question