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

Michael Neuling mikey at neuling.org
Wed May 23 07:05:50 GMT 2007


> 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.

coopers at tmp/ % cat a.sh
#!/bin/sh

test=abc123
coopers at tmp/ % cat b.sh
#!/bin/sh

. ./a.sh

echo test=$test
coopers at tmp/ % . ./b.sh 
test=abc123
coopers at tmp/ % 

Or am I missing something?

Mikey


More information about the linux mailing list