[clug] Amusing little /bin/sh question..

Zhasper zhasper at zhasper.com
Wed May 23 07:08:38 GMT 2007


". ./a.sh" will do what you want. It might be overly broad though:
"thevalueIwant=`something magic with a.sh`" implies that there's just one
value you want to extract from a.sh, whereas ". ./a.sh" will run the entire
script (and thus import all functions, variables, etc defined in a.sh). This
may clobber values you don't want to clobber..

The standard workaround is to do ". ./a.sh" as the very first action in b.sh;
that way, anything you define later replaces the 'default' value you
specifed in a.sh

Look in /etc/init.d on any RedHat machine for zillions of examples of this:

# Source function library.
. /etc/init.d/functions

If you think . is too terse, it's a synonym for "source", so use that verb
instead.

-- 
There is nothing more worthy of contempt than a man who quotes himself -
Zhasper, 2004

On 23/05/07, Andrew Janke <a.janke at gmail.com > wrote:
>
> Hi all,
>
> Just a quick one that has be somewhat bemused..
>
> If I have one shell script (a.sh) as such:
>
> ----
> #! /bin/sh
>
> set -e
> set -o nounset
>
> somevar="a-value"
>
> <lots of other things with $somevar>
> ----
>
> How can I get what the value of somevar in another script, for example
> I can do this on the C/L:
>
>   mavis:dir$ set -n && a.sh && echo $somevar
>
> Things will sort of work but the question is how to do this within
> another shell script as such (b.sh)
>
> ---
> #! /bin/sh
>
> set -e
> set -o nounset
>
> thevalueIwant=`something magic with a.sh`
>
> ---
>
> Of course I could resort to dirty tricks such as this:
>
>    thevalueIwant=`grep somevar a.sh | head -1 | cut -f2 -d\=`
>
> But then I am relying on myself not writing a comment about somevar
> and other things..  Of course I could do this....
>
>    thevalueIwant=`grep -v \# a.sh | grep somevar | head -1 | cut -f2 -d\=`
>
> but now we are getting really ugly.
>
> Just wanted to know if anyone knows a "nice" way to do this.
>
>
> ta
>
>
> a
>
> PS: answers of converting it to perl/python/ruby/etc are not
> considered answers! :)
>
>
> --
> Andrew Janke   (a.janke at gmail.com || http://a.janke.googlepages.com/)
> Canberra->Australia    +61 (402) 700 883
> --
> linux mailing list
> linux at lists.samba.org
> https://lists.samba.org/mailman/listinfo/linux
>
>


More information about the linux mailing list