Link to home
Start Free TrialLog in
Avatar of RegProctor
RegProctorFlag for United States of America

asked on

Shell source or dot command question

Hi,

I'm at the point with my first shell script where it will be worth breaking it up into multiple scripts.

Suppose I had 4 scripts support1, support2, support3 and main (all .sh scripts).

Now suppose support2 needs support1 and support3 also needs support1 and main needs support 2 & 3. This means in both support 2 & 3 scripts I would put '. ./support1.sh' and in main I would include scripts 2 & 3. Are there issues with this that I should be aware of? For example, will global variables in support1 set outside functions be set every time support1 is called/loaded?

In PHP these issues are resolved by use of "require_once" vs "require" (or the equiv. include cmds.) I don't see such commands in bash (I'm using OpenSuSE 11.0 which defaults to the bash shell).

Perhaps there is an "if script 'xxx' is loaded type command you can do so you only load a script once as needed? Or perhaps it doesn't matter? Perhaps there is a particular way that scripts work or are written that makes all this unimportant?

In any case, could someone please enlighten me?

Thanks.

Avatar of medvedd
medvedd

In bash when you call exernal script as "../support1.sh", the shell creates a subprocess (called child process). But you can source a script, and it will run in the current process. It can be done 2 ways (both are the same):

source ../script1.sh

or

. ../script1.sh
Avatar of RegProctor

ASKER

Yes, I know. My questions are a little beyond the basics of syntax of dot/source.
ASKER CERTIFIED SOLUTION
Avatar of Maciej S
Maciej S
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks heaps to all your good advice.