Creating an Easy Lookup Table in a UNIX or Linux Shell Script
A Script for Loading a Group of Variables with Environment-specific Values
I wanted to keep it simple so maintenance would be easy. I wanted it to run in a basic command shell (I use 'bash', but most other shells would work as well). UNIX and linux utilities like 'sed', 'awk,' and various xml parsers would have done the job, but they added complexity so I stayed away from them (although I did use 'grep'). The listing below is a simplified version of what I came up with. It takes one parameter representing the target environment, and sets 3 variables: the target server, the target database, and a process user ID. It then prints the new values to the screen for verification (an optional step). In reality, the unsimplified version of this script also defines target directories, service names, and website urls among other things, but this is enough to give you the idea:
Listing 1#!/bin/bash
# Sets environment variables based on lookup string
# Environments: DEV = Development, QA = Quality Assurance,
# UAT = User Acceptance Test, PROD = Production
ENVIRONMENT=$1
# Set server addresses, database names, and user IDs.
line=`grep ^$ENVIRONMENT --- ------------------------ --------- ----------
DEV dev.myapp.mybusiness.com myappdev devappuser
QA qa.myapp.mybusiness.com myappqa qaappuser
UAT uat.myapp.mybusiness.com myappuat uatappuser
PROD prod.myapp.mybusiness.com myappprod prodappuser
EOF`
set -- $line
export AppServer=$2
export DataBase=$3
export UserID=$4
#
# Show environment settings:
echo "AppServer = $AppServer"
echo "DataBase = $DataBase"
echo "UserID = $UserID"
Sample run:
$ ./Lookup.sh DEV
AppServer = dev.myapp.mybusiness.com
DataBase = myappdev
UserID = devappuser
$
Using the Technique
Knowing how this script works is not essential to using the technique, as long as you realize that you can expand it by adding more values to the ends of the input lines, and creating enough values with the 'export' statements to accomodate the new values. If you have more than 4 target environments, you simply create more input lines, using the first word in each line to match the additional environment identifiers. If you want to understand what's happening, so that you can expand upon and evolve the technique, read on.
The first key to understanding this script is knowing how redirection works. In shell scripts as in DOS, '>'s redirect standard output, usually to a file. '>>' also redirects output to a file, but appends to the end of the file instead of over-writing it. 'The second key to understanding this script is knowing that the back-ticks '`' tell the shell to execute everything between them and return the result as a string. In our case, the back-ticks enclose the grep command and the input we're feeding to grep, so the shell will execute this and assign the resultant string to our variable 'line.' So, in our example run, we passed in the word 'DEV' as the parameter, and assign it to the variable $ENVIRONMENT. We then send all the text between the EOF strings in the script to grep, which only returns the one line beginning with 'DEV.' It assigns this line to the variable $line.
The third and final key to this script is the 'set' line. You may know that parameters you pass into a script on the command line are automatically assigned to the general variables $1, $2, $3, and so on. That's how we got the $ENVIRONMENT variable assigned. 'set' has a number of functions, but this script is only using one, which is to re-assign those general variables to the values of the given variable, $line. set -- $line does this. So, in the example run, $line contains the input line matching 'DEV'. $1 is assigned the first word ('DEV'), $2 is assigned 'dev.myapp.mybusiness.com', $3 gets 'myappdev', and $4 gets 'devappuser.' Then it's just a matter of giving those values meaningful variable names, and dumping them to the screen with the 'echo' commands.
There you have it. A simple Lookup Table, implemented in a shell script, which can be used to assign a group of values to a set of variables based on an input parameter. You can then use these variables to deploy your applications to a number of different environments without a lot of complex if-then-else logic to determine environment-specific settings.
Published by Joe Poniatowski
A full time IT consultant with over 20 years experience. Clients have included 2 of the big 3, financial institutions, and state and local governments. View profile
- Is a Support Group Right for Me?This article explores what a support group is and might help you determine if a support group is right for you.
- Alternative Hip-Hop Group The Wylde Bunch Rocks Sports ArenasWylde Bunch, a 10-member alternative hip-hop group from Los Angeles, released their newest self-titled EP this week. Its sports anthems and upbeat lyrics establish the album as a staple of athletic events across the c...
- How to Start a Writers GroupTen easy steps toward creating a writers group in any area. A writers group can be a vital help in getting published and it is much easier than most writers think to start a group.
- Reduce Stress with a Soothing EnvironmentReducing stress is a challenge we all face on a daily basis. Choosing elements that lead to a soothing environment can help you lead a better quality of life.
- More Easy Spanish RecipesTraditional dishes in easy and fast recipes
- Navigating Unix and Linux Shell Programs
- Learning Linux in the Fast Lane
- Right-Wing Christian Group Ethur Raises Issue with Nintendo's Wii
- What is a Marketing Research Test Group Member?
- Senior Portrait Photographer - Why You Should Form a Focus Group
- Tony Dungy Stands for Pro Family Group Despite Attacks
- Free Advice for Yahoo Group Moderators




12 Comments
Post a CommentThank you so much for this useful info!!!
Excellent information. Thank you
I used to take UNIX, but it is out of my head now...
I've been playing with command shells. Stop me before it is too late..
:)
hey ... were you in my head last night? i need to brush up on my UNIX and shell scripting :-)
A wonderful read! Outstanding job!
This was the most fun I've had reading an article in a long time. Wow..... you really know your stuff! I haven't delved into all of the wonderful things that can be done with a command shell, but would love to. I just don't have the time it seems to play with it.
Bah - I can't even post the code listing lines in the comment section, even though it isn't web code. See the un-mangled code listing here: http://1003concepts.com/jp/publications/30
Joe -
I don't understand this all that well but I sure wish I did. I am sure you have found a niche topic here and it will someone's need exactly. :)
P.S. Thanks for your very helpful advice on the forum. I'm so glad people like you show up there!
Some bits I understand...some I don't , but I'm working on it. Thanks.