Operating System - HP-UX
1827703 Members
2966 Online
109967 Solutions
New Discussion

Manage shell scripting environments

 
SOLVED
Go to solution

Manage shell scripting environments

Hello all,

I have taken over a couple of shell scripting suites and would like to know if there are any best practice guides or general practices on how to best manage large shell scripting environments while keeping them flexible?
In my case I have a large set of live scripts where most things are hard coded (paths and databases for example) and scripts are piggy backed onto each other (script1 calls script2 calls script 4 and so on...). To create a test environment is bit of a nightmare, pretty much every script have to be 'tweaked' to work in test.
I am looking to re-write quite a lot of it to make it workable and maintainable long term.

All ideas and advice would be most appreciated.

Thanks
Matt
4 REPLIES 4
N,Vipin
Frequent Advisor

Re: Manage shell scripting environments

.The best way is to declare the varibales in the beggining of every script, it will help us to change the values later time easly.
.Use funactions to keep the scripts as simple as much.
James R. Ferguson
Acclaimed Contributor

Re: Manage shell scripting environments

Hi Matt:

Consider establishing includable files of common variables that you can source (read) as needed. This is done with the 'dot-space-filename' syntax so that no new environment is created. This approach can be used to conditionally choose to include either a production or a test suite of variable values based on a runtime argument.

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor
Solution

Re: Manage shell scripting environments

Hi (again) Matt:

You could also build an includable file of functions that you want to deploy into more than one script. This file might look like:

# cat /home/matt/functions
#!/usr/bin/sh
function tattle
{
echo "tattle() saw $@"
}
function listdir
{
echo "current directory : $PWD"
ls
}
#_end_of_functions_

...and you could deploy it like:

# cat ./mytest
#!/usr/bin/sh
. /home/matt/functions
tattle "Mattias Johnsson"
cd /var/tmp && listdir
exit 0

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: Manage shell scripting environments

Another technique is to use a separate config file. I recently wrote a script to replace sendmail for sending authenticated email. With more than 100 MB of addons (KRNG, OpenSSL, CyrusSASL), it seemed overkill just to perform a simple authentication. The script started out with a lot of hardcoded variables but it became apparent that this would be useful in many environments. So I moved all the configurable variables to an /etc config file like this:

## config file
MAILRELAY=abc.xyz.com
MAILRELAYPORT=587
AUTHPLAIN="sdkchweiy43yutewurkw786h50ythbo="
REPLYTO=bill@bill.com

And just for extra credit, I added checks for these variables already preassigned so that the config file would only be used for default values.


Bill Hassell, sysadmin