Solved

How to set and use external variables in shell scripts?

Posted on 2009-04-03
6
302 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:trh_garr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 24063050
you can always source the file with

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

Expert Comment

by:woolmilkporc
ID: 24063080
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



0
 

Author Comment

by:trh_garr
ID: 24063113
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:trh_garr
ID: 24063137
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. :)
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 24063189
source means that the sell script that source will read and execute it in the current shell context and hence if there is an env variable setting then it will read it and set it in its env.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 24063203
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)

0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question