Re: Bash – ”Sourcing” script from another script
Kevin O'Gorman
kogorman at gmail.com
Sun Jan 4 22:44:49 UTC 2015
I can imagine arguments in both directions, so I regard this as a matter of
taste. You'll know, because if this turns into a flame war, I was right.
There are a number of things you can do in scripting languages that make no
sense elsewhere. I have, for instance, a home-grown set of backup scripts
that adapt to different hosts by sourcing a script named after the host
it's on. So some details of what gets backed up depend on where it's
running, but the boilerplate (backing up the root filesystem, for instance)
is in common. There are other ways to do this, of course, but my way keeps
the per-site scripts to a very bare minimum. I'd never think of doing this
in a compiled language.
If the parts are conceptually different enough, separating them is a
natural modularity.
If they're very tightly coupled, the separation can make it hard to
understand the flow.
YMMV.
There's more than one right answer.
Or, my dear Mom's favorite: "To each his own, said the old lady as she
kissed the cow."
On Sun, Jan 4, 2015 at 2:09 PM, Johnny Rosenberg <gurus.knugum at gmail.com>
wrote:
> Just wondering…
> I have a script that does two different things related to each other. I
> could say that the script does one thing in two distinct steps, so it feels
> natural to split those things into two scripts.
>
> However, the second part of the script use some variables that got its
> values in the first part, so the second script would need to calculate
> those variables again, which feels redundant.
>
> Seems like ”source” would do the job, since it executes the script in the
> same shell. I thought something like this:
>
> ScriptA.sh:
> SomeVariable=Something
> AnotherVariable=SomethingElse
> Do-stuff
> source ScriptB.sh
>
> ScriptB.sh:
> source Some-function-defs.sh
> SomeFunction "$SomeVariable"
> AnotherFunction "$AnotherVariable"
> Do-more-stuff
>
> Some-function-defs.sh:
> SomeFunction () { Some action}
> AnotherFunction () { More action }
>
> I'm pretty sure it will work, but is this a good practice?
> Is it better to keep everything in one file even if that file will be
> quite big?
> Or is it better to just run the script in another shell sending it a lot
> of parameters?
>
>
> Kind regards
>
> Johnny Rosenberg
> ジョニー・ローゼンバーグ
>
>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>
>
--
Kevin O'Gorman
#define QUESTION ((bb) || (!bb)) /* Shakespeare */
Please consider the environment before printing this email.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20150104/186ce793/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/gif
Size: 441 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20150104/186ce793/attachment.gif>
More information about the ubuntu-users
mailing list