RegProctor
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.
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.
ASKER
Yes, I know. My questions are a little beyond the basics of syntax of dot/source.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks heaps to all your good advice.
source ../script1.sh
or
. ../script1.sh