Link to home
Start Free TrialLog in
Avatar of trh_garr
trh_garr

asked on

How to set and use external variables in shell scripts?

We have 4 layers(dev, tst, at, pd) on the same server where a korn shell script will run on all 4 layers.

I want to make the script generic where I have 4 copies of the same script, one on each layer. I'd like to determine which layer the script is running from using a form of environment variable. I've been told I can create an environment file that will exist on each layer with it's correcponding layer value and then refer to that variable in the shell script.

As I'm not proficient in unix, I need to know how I may be able to do this,

For instance I can create the following ksh file that would reside in the same location as the shell script:
#!/bin/ksh

ENV_LAYER=dev     # this env variable needs to be updated to dev, tst, at, pd
export ENV_LAYER

But then I don't know how or if I can access this variable in the script???

Thanks.
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

you can always source the file with

. /path/to/file (please note the dot and space)
Hi,

for each layer create a file containing

ENV_LAYER=[layer]

name it e.g. layer.conf and make it executable (chmod +x layer.conf)

In the common script simply do

.  /path/to/layer.conf

and use the variable ENV_LAYER as before.

Note the dot[space] (. ), it is very important.

wmp



Avatar of trh_garr
trh_garr

ASKER

omarfarid:

you can always source the file with

. /path/to/file (please note the dot and space)

Sorry but I don't know what you mean "source the file".

Told you I wasn't unix proficient
Told you I wasn't unix proficient

I just re read this an it doesn't sound the way I intended, sorry.

What I was trying to say is that I REALLY am not proficient in unix. :)
ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Another approach is to simply have a configuration file, eg: /etc/layer.conf

with the contents dev, tst, at or pd, eg:

dev

then in your script you do

ENV_LAYER=$(cat /etc/layer.conf)