1

I would like to queue some bash scripts on a file that I can modify. So that I launch that file "lauchscripts.py" or "lauchscripts.sh" which contains the commands for launching the scripts:

1:> source folder1/script1.sh 
2:> source folder2/script2.sh
3:> source folder3/script3.sh 
4:>

So that: 1) when I launch this, it launches script1.sh, then when it finishes it launches script2.sh and so on. 2) at anytime while running,I can add lines to my "lauchscripts" file that will be later executed. Let's say while running script3.sh (line 3), if I add at line 4 a command:

1:> source folder1/script1.sh 
2:> source folder2/script2.sh
3:> source folder3/script3.sh 
4:> source folder4/script4.sh

This line will execute script4.sh after finishing script3.sh

Is that possible?

Thanks

music2myear
  • 40,472
  • 44
  • 86
  • 127
nikarko01
  • 11
  • 1
  • Related: [*Edit shell script while it's running*](https://stackoverflow.com/q/3398258). Just appending to the file may work, I wouldn't make this a crucial part of my workflow though. – Kamil Maciorowski May 30 '18 at 07:56
  • If you'd like to run (not source) these scripts (or other commands) and you're on Unix, see [this question](https://superuser.com/q/220364/432690). Task Spooler looks promising. – Kamil Maciorowski May 30 '18 at 11:11

1 Answers1

0

If your file contains the names of the scripts themselves rather than code, you can do this easily and safely:

while read -r -u 9
do
    . "$REPLY"
done 9< scripts.txt

As with most scripting hacks, there are a lot of caveats to this (and even more if you read code and eval it):

  1. If file descriptor 9 (-u 9) is used for anything in any of the scripts all bets are off.
  2. If any of the scripts set any variables this context not be cleared out.
  3. Once the last line of scripts.txt is read the loop finishes.

A better solution would be to run the scripts you read, by simply removing the dot. For this to work the script paths need to contain at least one slash or else the scripts need to be on your PATH.

l0b0
  • 7,171
  • 4
  • 33
  • 54
  • [I tried it](https://github.com/Nikarko01/example), and it look like when running the `while read` code, it reads all in once, instead of reading as executing. Hence changing **scripts.txt** while running your lines, won't affect what will be run. – nikarko01 May 30 '18 at 09:33