Sunday, February 5, 2012

A simple way to view shell environment variables

0 comments
Most of the time when you deploy or run a bigger application or writing a complex program you'll find you have problems with your environment. You'll find everything from environment variables not set or not set correctly to missing resources.

In order to quickly check the settings of environment variables I've created a simple python shell utility to do that for me.

I called the utility evar for "environment variable". It lets me put a list of environment variable behind the script name and have my environment checked for the presence of each of them in by upper and lower case forms.

Example:
[gail@gail]$ evar home AFWE qwer foo 

HOME                           /home/gail                              
AFWE                           IS NOT SET                              
QWER                           IS NOT SET                              
FOO                            FOOOOO                                  
foo                            bar

Additionally, when we get long path listings it can be difficult to find the element we are searching for with all of the elements on one line. So in the script I check for the presence of colons ':' in the line then format the print output so that there is one element to a line. This breaks up the path so that it can be more easily read and make it easier to find the elements we are looking for.

That means that two variables defined as:

[gail@gail]$ echo $FOO
FOOOOO

[gail@gail]$ echo $foo
asdf:qwerqwer:zxcvzxcv

Will look like this when formatted by evar:

[gail@gail]$ evar foo

FOO                            FOOOOO                                  
foo
                               asdf                                    
                               qwerqwer                                
                               zxcvzxcv 

The script's listing follows. You can do what you what you want with the formatting. I like the breathing room and the ordered lay out.

#!/usr/bin/python

import os, sys, re
print 
for ii in sys.argv[1:]:
        var = ii.upper()

        if var not in os.environ and ii not in os.environ:
                print "%-30s %-40s" % (var,'IS NOT SET')

        if var in os.environ:
                mvar = os.environ[var]
                if re.search(':',mvar):
                        print var
                        for val in mvar.split(':'):
                                print "%-30s %-40s" % ('',val)
                else:
                        pvar = os.environ[var]
                        print "%-30s %-40s" % (var,pvar)


        if ii in os.environ:
                mvar = os.environ[ii]
                if re.search(':',mvar) is not None:
                        print ii
                        for val in mvar.split(':'):
                                print "%-30s %-40s" % ('',val)
                else:
                        pvar = os.environ[ii]
                        print "%-30s %-40s" % (ii,pvar)


print 

Comments

0 comments to "A simple way to view shell environment variables"