Re: Bash – ”Sourcing” script from another script

Johnny Rosenberg gurus.knugum at gmail.com
Mon Jan 5 17:16:11 UTC 2015


2015-01-04 23:50 GMT+01:00 Karl Auer <kauer at biplane.com.au>:

> On Sun, 2015-01-04 at 23:09 +0100, Johnny Rosenberg wrote:
> > I'm pretty sure it will work, but is this a good practice?
>
> It'll work, but it introduces dependencies that are a bit hard to
> follow.
>
> If possible, I would refactor the scripts into three - one script
> calc.sh that calculates the variable values, then two separate scripts
> script1.sh and script2.sh that each source calc.sh.
>

Another idea I had, before I read this, was four scripts:

MainScript.sh
CalculateVariables.sh
Script1.sh
Script2.sh

MainScript.sh would look something like this:
#!/bin/bash

CalculateVariables.sh
Script1.sh
Script2.sh
MoreStuff here or where ever suitable.

Could that be a reasonable approach?
Script1.sh and Script2.sh could have commants that tells the reader that
variables are declared and/or calculated in CalculateVariable.sh and that
MainScript.sh starts all the other scripts, just to make it easier to
follow.


Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ



>
> It depends on how complicated/expensive those variables are to
> calculate, and whether calculating them has side-effects.
>
> > Is it better to keep everything in one file even if that file will be
> quite
> > big?
>
> If two scripts have a section that does the same thing in each script,
> and that thing is non-trivila, it's generally a good idea to separate it
> out and get it right once. Then if changes are ever needed, you only
> have to change one place and it will be corrected in all the places that
> use it.
>
> Regards, K.
>
> --
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Karl Auer (kauer at biplane.com.au)
> http://www.biplane.com.au/kauer
> http://twitter.com/kauer389
>
> GPG fingerprint: 3C41 82BE A9E7 99A1 B931 5AE7 7638 0147 2C3C 2AC4
> Old fingerprint: EC67 61E2 C2F6 EB55 884B E129 072B 0AF0 72AA 9882
>
>
>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20150105/93720141/attachment.html>


More information about the ubuntu-users mailing list