Creating an Easy Lookup Table in a UNIX or Linux Shell Script

A Script for Loading a Group of Variables with Environment-specific Values

Joe Poniatowski
As a Configuration Manager, I'm always looking for ways to improve the automation of the builds and deployments of my company's applications. We use scripts to compile the apps, replace certain token strings with environment-specific values, and copy the new executable code out to the production servers. Ideally, we should not have to use separate scripts when deploying to different run-time environments (development, integration test, production, etc.). We want instead to pass the target environment into these scripts, and use logic to determine environment-specific values. So I set out to create a Lookup Table to set the values according to the target environment.

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

Shell scripts in UNIX and linux provide a rich set of redirection substitution features, making them well suited for environment-specific tasks.

12 Comments

Post a Comment
  • samaira9/6/2009

    Thank you so much for this useful info!!!

  • Rae Lynne Morvay3/29/2008

    Excellent information. Thank you

  • IcyCucky2/28/2008

    I used to take UNIX, but it is out of my head now...

  • jcorn2/28/2008

    I've been playing with command shells. Stop me before it is too late..
    :)

  • Nikki2/27/2008

    hey ... were you in my head last night? i need to brush up on my UNIX and shell scripting :-)

  • Rodney Southern2/22/2008

    A wonderful read! Outstanding job!

  • Kim Linton2/21/2008

    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.

  • Joe Poniatowski2/20/2008

    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

  • jcorn2/15/2008

    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!

  • Louie Jerome2/14/2008

    Some bits I understand...some I don't , but I'm working on it. Thanks.

Displaying Comments
Next »

To comment, please sign in to your Yahoo! account, or sign up for a new account.