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

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

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`

Related information
Shell scripts in UNIX and linux provide a rich set of redirection substitution features, making them well suited for environment-specific tasks.
 
Comments 1 - 10 of 11 Next >>
Comments
Type in Your Comments Below

Excellent information. Thank you

Posted on 03/29/2008 at 10:03:52 AM

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

Posted on 02/28/2008 at 12:02:12 PM

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

Posted on 02/28/2008 at 7:02:36 AM

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

Posted on 02/27/2008 at 2:02:13 PM

A wonderful read! Outstanding job!

Posted on 02/22/2008 at 8:02:34 PM

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.

Posted on 02/21/2008 at 2:02:32 PM

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

Posted on 02/20/2008 at 2:02:38 PM

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!

Posted on 02/15/2008 at 9:02:02 PM

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

Posted on 02/14/2008 at 2:02:40 PM

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

Posted on 02/14/2008 at 2:02:36 PM

Comments 1 - 10 of 11 Next >>