Operating System - HP-UX
1822430 Members
3299 Online
109642 Solutions
New Discussion юеВ

Environment variables in shell script

 
ZezinhoBD
Occasional Contributor

Environment variables in shell script

I have one script which has some variables set and I need to use them outside the script, i.e., I will call a standard script through several other scripts and I have to re-use the main one to avoid errors.

Is it possible in HP-UX 11.

For example:

1st script ( env.sh )
DBCONNECTION=/usr/local/etc/param.pf; export DBCONNECTION

2nd script ( dbprod.sh )
sh env.sh
mpro -pf $DBCONNECTION

When I call the 2nd script the environment variables as empty and the application doens't start.

Please, help me
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: Environment variables in shell script

Hi:

You need to 'source' or read your variables. For example:

# cat ./sh1
#!/usr/bin/sh
DBCONNECTION=/usr/local/etc/param.pf

# cat ./sh2
#!/usr/bin/sh
. ./sh1
mpro -pf ${DBCONNECTION}

That is, you use a dot (".") operator followed by a blank (space), followed by the name of the file to be 'source'd (read, or included).

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: Environment variables in shell script

And the reason is that when you "call a
standard script", you're actually running
that shell script in its own process, and
that shell process has its own environment.
Setting an environment variable in that
shell's environment does nothing to the
environment of the original shell's process.

Using the "." command causes the original
shell to read and execute the second script's
commands, so those commands now affect the
environment of the original shell process
(which is the only process in this case).

Note, too, that when using ".", the second
"script" file does not need to be executable,
and it does not need a "#!shell_path" on its
first line. (That's because it's not really
being used as a shell script, only as a kind
of data file for the first shell script.)

Of course, if that second file _is_
executable then you can also use it as a
shell script, and then you'll probably want
to include a "#!shell_path" in it.
SANTOSH S. MHASKAR
Trusted Contributor

Re: Environment variables in shell script

Hi,

As suggested by James u have to run env.sh in
the same shell, if u r using

sh env.sh

then a child shell process is created,
the exported variables in env.sh will be
available to processes which r child of shell
running env.sh, hence after

sh env.sh

these variables r distroyed, to prevent this
and make ur exported variables in same shell
use

. env.sh

using . (dot) will tell shell to run process in
current shell only, thus all the shell
variables in env.sh will be available in ur
2nd script dbprod.sh


-Santosh
Arturo Galbiati
Esteemed Contributor

Re: Environment variables in shell script

Hi,
export the varaiable in the first script. execute it as '.' script, and you will be able to see the variable in teh other script:
1:
/> cat test1
#!/usr/bin/ksh
export A="I'm A"
#eof

2:/> cat test2
#!/usr/bin/ksh
. test1
echo "I'm test2"
echo $A
#eof

3:> test2
I'm test2
I'm A

HTH,
Art

Vibhor Kumar Agarwal
Esteemed Contributor

Re: Environment variables in shell script

Use source command if you are using C shell.
Vibhor Kumar Agarwal
ZezinhoBD
Occasional Contributor

Re: Environment variables in shell script

The answer from James R Fergusson solve my problem.

Tks.