Operating System - HP-UX
1833758 Members
2529 Online
110063 Solutions
New Discussion

how to provide input from current env to new env

 
yogesh kumar_2
Frequent Advisor

how to provide input from current env to new env

hi

Suppose i am executing one script in(/home/XXX).So in that script i am calling another script (For ex:ae_init_sandbox).After running this script(ae_init_sandbox $sandboxname) it is entering into new environment.How to execute another script in the new environment.

ex:
1.sh script--inside script contain ae_init_sandbox $sandboxname
2.ae_init_sandbox(script)
3.It is entering into that particular sandbox(i,e new environment)
4.I want to execute one script in new environmet.

what is the procedure to solve the above cases?


5 REPLIES 5
Kenan Erdey
Honored Contributor

Re: how to provide input from current env to new env

Hi,

use export command to carry the value to child shell.

Kenan.
Computers have lots of memory but no imagination
TwoProc
Honored Contributor

Re: how to provide input from current env to new env

As Keenan said - you can use export:

file "scripta"
----------------------------------
export MYVARA = "hello"
/home/user/scriptb
----------------------------------

file "scriptb"
----------------------------------
echo $MYVARA
----------------------------------

So from the command line if you ran
./scripta from the /home/user directory:
you get "hello" as output

$> ./scripta
hello

If you didn't use "export" first, it wouldn't work. Note that you can export AND declare a variable at the same time in ksh and bash, but not in sh. In sh, in script a you have to do it in two lines like:
MYVARA="hello"
export MYVARA

It's safer to do this the way above, because this always works in bash, ksh, and sh.

Another way to get this done is just pass an argument, and pick it up as $1 in the second script:

file "scriptc"
-------------------------------------------
MYVARC="goodbye"
/home/user/scriptd $MYVARC
----------------------------------------

file "scriptd"
----------------------------------------
echo $1
----------------------------

The run would look like:
$> /home/user/scriptc
goodbye

This is basic stuff, but it sounded to me like this is what you were wanting. If not, please excuse the posting.

HTH
We are the people our parents warned us about --Jimmy Buffett
James R. Ferguson
Acclaimed Contributor

Re: how to provide input from current env to new env

Hi:

Another way to accomplish your goal is to source (read) your variables into your environment from another file. Sourcing (reading) is done by specifying a dot ('.') followed by whitespace, followed by a file to be read:

# cat myscript
#!/usr/bin/sh
WHO=yogesh
echo "I am '${WHO}'"
echo "hi"
. /home/yk/myincl
echo "I am also known as '${NAME}'"
echo "bye"

# cat myincl
#!/usr/bin/sh
NAME=kumar

...run:

# ./myscript
I am 'yogesh'
hi
I am also known as 'kumar'
bye

Note that failure to use the 'dot-whitespace-filename' form will cause a separate environment to be created and you will not have the new environmental variables available.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: how to provide input from current env to new env

>TwoProc: Note that you can export AND declare a variable at the same time in ksh and bash, but not in sh.

You're confused. sh IS the posix shell, which is a superset of ksh. Only the bourne shell is junky like that.
TwoProc
Honored Contributor

Re: how to provide input from current env to new env

Thank Dennis for clearing that up. I don't know how it is I've gone so long with that misunderstanding, an old experience with something mistranslated to a hard and fast (wrong) "rule", I think.
Thanks.
We are the people our parents warned us about --Jimmy Buffett