|
Home > Archive > Unix Shell > February 2005 > Running scripts using variables in a parent script
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Running scripts using variables in a parent script
|
|
|
| Is it possible to call a script from within another script, but have
the "child" script use the "parent" script's variables?
What I'd like to do is let users customize a small text file that
contains a little portion of script, then a main script would be able
to call that script and use variable values that are from the parent
script.
The real script would have lots and lots of variables, but a
simplified example of what I'd like to do is below.
Simplified Example:
Script1:
my_name="Bob"
/script/location/Script2
Script2:
echo "My name is ${my_name}."
So running Script1 would echo "My name is Bob."
Thanks!
| |
| Chris F.A. Johnson 2004-04-19, 5:35 pm |
| On Mon, 19 Apr 2004 at 21:14 GMT, Ken wrote:
> Is it possible to call a script from within another script, but have
> the "child" script use the "parent" script's variables?
>
> What I'd like to do is let users customize a small text file that
> contains a little portion of script, then a main script would be able
> to call that script and use variable values that are from the parent
> script.
>
> The real script would have lots and lots of variables, but a
> simplified example of what I'd like to do is below.
>
> Simplified Example:
>
> Script1:
> my_name="Bob"
> /script/location/Script2
>
> Script2:
> echo "My name is ${my_name}."
>
>
> So running Script1 would echo "My name is Bob."
In Script1, put:
export my_name
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
| |
| Barry Margolin 2004-04-19, 6:34 pm |
| In article <2b0a8b3b.0404191314.4031e58d@posting.google.com>,
morau99@hotmail.com (Ken) wrote:
> Is it possible to call a script from within another script, but have
> the "child" script use the "parent" script's variables?
>
> What I'd like to do is let users customize a small text file that
> contains a little portion of script, then a main script would be able
> to call that script and use variable values that are from the parent
> script.
>
> The real script would have lots and lots of variables, but a
> simplified example of what I'd like to do is below.
>
> Simplified Example:
>
> Script1:
> my_name="Bob"
> /script/location/Script2
>
> Script2:
> echo "My name is ${my_name}."
>
>
> So running Script1 would echo "My name is Bob."
>
> Thanks!
The main script should export the variables. Or it could execute the
child script using ". <scriptname>", e.g.
my_name="Bob"
.. /script/location/Script2
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
| |
| Icarus Sparry 2004-04-20, 1:34 am |
| On Mon, 19 Apr 2004 14:14:46 -0700, Ken wrote:
> Is it possible to call a script from within another script, but have
> the "child" script use the "parent" script's variables?
>
> What I'd like to do is let users customize a small text file that
> contains a little portion of script, then a main script would be able
> to call that script and use variable values that are from the parent
> script.
>
> The real script would have lots and lots of variables, but a
> simplified example of what I'd like to do is below.
>
> Simplified Example:
>
> Script1:
> my_name="Bob"
> /script/location/Script2
>
> Script2:
> echo "My name is ${my_name}."
>
>
> So running Script1 would echo "My name is Bob."
As others have said, you want to export your variables.
You mention that you have lots of them, so you may want to use
set -a
instead of putting in lots of export statements. You would be in good
company. When the people who wrote Unix decided that it was time for
something better one of the changes they made was to replace /bin/sh with
a new shell 'rc'. This shell exports all variables automatically. One
reason the original shell didn't was because the amount of space that was
available for passing arguments and environment variables to sub-processes
was pretty limited. These days the limits still exist, but are much
bigger, e.g. Linux allows 128Kb and Solaris8 allows 1Mb.
The paper http://www.cs.bell-labs.com/sys/doc/rc.pdf describes 'rc' more,
and there is the implementation available from http://www.star.le.ac.uk/~tjg/rc/
Personally I have 'set -a' in my profile, it is just one less thing to
have to think about.
| |
| Brian Gough 2004-04-20, 5:34 am |
| morau99@hotmail.com (Ken) writes:
> Is it possible to call a script from within another script, but have
> the "child" script use the "parent" script's variables?
>
> What I'd like to do is let users customize a small text file that
> contains a little portion of script, then a main script would be able
> to call that script and use variable values that are from the parent
> script.
You can use the "export" shell command to mark any variables you want
to export to subshells. By default, variables are not exported. HTH.
--
Brian Gough
Network Theory Ltd,
Publishing the GNU Bash Reference Manual --- http://www.network-theory.co.uk/
| |
| Kevin Rodgers 2004-04-20, 1:34 pm |
| Icarus Sparry wrote:
> Personally I have 'set -a' in my profile, it is just one less thing to
> have to think about.
But then all scripts share the same global namespace, which introduces
dependencies between them. In particular, declaring a variable to be of
a certain type in one script can interfere with its use in another
script.
So from my perspective, it's one more thing to worry about.
--
Kevin Rodgers
| |
| budman 2004-04-25, 1:34 pm |
| On Tue, 20 Apr 2004 05:28:50 +0000, Icarus Sparry wrote:
> On Mon, 19 Apr 2004 14:14:46 -0700, Ken wrote:
>
> reason the original shell didn't was because the amount of space that was
> available for passing arguments and environment variables to sub-processes
> was pretty limited. These days the limits still exist, but are much
> bigger, e.g. Linux allows 128Kb and Solaris8 allows 1Mb.
>
Is there a command to check the size limit and command line limit?
Rich
| |
| Icarus Sparry 2004-04-29, 11:34 pm |
| On Sun, 25 Apr 2004 14:13:02 -0300, budman wrote:
> On Tue, 20 Apr 2004 05:28:50 +0000, Icarus Sparry wrote:
>
>
>
> Is there a command to check the size limit and command line limit?
Shells should not limit you at all. Typically the only way to find out is
to look at their source, but you could try reading their man pages.
To find the limit on exec, you can try running
getconf ARG_MAX
or else look for ARG_MAX in /usr/include/limits.h.
| |
| Icarus Sparry 2005-02-24, 2:58 am |
| On Sun, 25 Apr 2004 14:13:02 -0300, budman wrote:
> On Tue, 20 Apr 2004 05:28:50 +0000, Icarus Sparry wrote:
>
>
>
> Is there a command to check the size limit and command line limit?
You can try
getconf ARG_MAX
to find the limit for the 'exec' system call. Shells *should* not impose
any other limits, but if they do the best place to look is either their
source or else the manual pages.
|
|
|
|
|