Solved

How to set and use external variables in shell scripts?

Posted on 2009-04-03
6
292 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
6 Comments
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
you can always source the file with

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

Expert Comment

by:woolmilkporc
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:trh_garr
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now