Solved

How to set and use external variables in shell scripts?

Posted on 2009-04-03
6
298 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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 …
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:

839 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