Solved

How to set and use external variables in shell scripts?

Posted on 2009-04-03
6
301 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.
Suggested Courses

734 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