0

I'm new in Unix/Gnu/Linux and trying learn some more about it.

I have a case where I need to run 2 scripts inside a loop, but these 2 scripts need to run sequentially.

For example:

TargetFile = /data/TEST.CSV

while read -r line || [ -n "$line" ]; do script_copy_file.sh $Parm1 $Parm2 $Parm3 script_split_file.sh $Parm1 $Parm2
done < "$TargetFile"

Do I need to put something in between those 2 scripts so the script_split_file.sh will run after script_copy_file is finished? I read some article to use &&, but I'm here to seek some advice about the best practice for it.

Or will it naturally run in sequence?

Sorry if my question is funny or weird, I just try to learn and understand. Really appreciate for your help.

Thank you.

2 Answers2

0

The two scripts will run one after the other if you invoke them on separate lines in a script, without anything but a newline between them, like you're showing in the question.

You only want to put && between them if you want the second script to only run if the first script end successfully.

With a newline (or ;) between the invocations, the second script will run regardless of whether the first script ran successfully or not.

Using || between them will make the second script run only if the first script fails (returns a non-zero exit status).

Looking at your code, you will also want to double-quote your paramaeter expansions:

script_copy_file.sh "$Parm1" "$Parm2" "$Parm3"
script_split_file.sh "$Parm1" "$Parm2"

... and assignments should have no spaces around the =.

Related:

Kusalananda
  • 333,661
0

It will run:

script_copy_file.sh
script_split_file.sh

script_copy_file.sh script_split_file.sh

script_copy_file.sh script_split_file.sh …

If you want it to run

script_copy_file.sh
script_copy_file.sh
script_copy_file.sh

script_split_file.sh script_split_file.sh script_split_file.sh

Then you need two loops.

Other

You need to quote variables, consider are you using the correct variables, not have a space before the = in the assignment, and use shellcheck to check your scripts.

Use of .sh at the end of file-names is also a bad idea, it will cause problems maybe not to day, maybe not tomorrow, but someday. Imagine how may scripts you will have to rewrite when you re-implement one of your scripts in python.