Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

configuration files 1

Status
Not open for further replies.

richy00

MIS
Nov 13, 2000
21
US
I would like to create a configuration file where I can place predefined variables for my shell script(s) to use instead of having to change every script to point to say, a log directory, every time I wanted to change the destination of where I would like my log files kept when they were generated.

Example 1:

I would like for my shell script “go.sh” to use the variables defined in the configuration file “config.conf”

Example 2:

Configuration file (config.conf)-
-Contents-

LOGS=/usr/logs

Shell Script (go.sh)-
-Contents-

#!/bin/ksh
print “The location of the log file directory is, $LOGS”


STOUT-

The location of the log file directory is, /usr/logs

Is there a mechanism that can read all of the variables that are defined on each line of the configuration file, so that all I have to do is use the variable name inside of my shell script and the value will be used during runtime of the script? If possible, what would the syntax be?
 
There are a couple of ways you could do this I suspect. The easiest way is to simply put the variables in the .profile of the user who would run the scripts. The second way I can think of is to have the .profile or each script which runs call/run the configuration file which would set the environment. The variables would then be available in each script which runs. Hope this helps.

- Stuart
 
Good point, that would work well, but I'm looking at doing something a little different. Say a co-worker wanted to use this script, he would have to update his .profile too in order to gain the same effects. I would like to eliminate the high maintnance of having to do that, instead I would rather just utilize a configuration file to maintain the variable values.
 
call a script to do it
. /usr/local/bin/setup_for_richy

Tony ... aka chgwhat
tony_b@technologist.com

When in doubt,,, Power out...
 
richy00,

The second option was also (as chgwhat said) to call/run the configuration file (setup_for_richy) from the script(s) which are to be run. For example, a user wants to run the script called "test_tape.sh". The script (test_tape.sh) would contain the line to run the configuration file as follows:

#!/bin/sh
/usr/local/bin/setup_for_richy # call config file
mt -f $DEVICE status # check tape drive

The $DEVICE variable would be obtained from calling the config file (setup_for_richy) in the second line of the script. Hope this clears it up.

Regards.

- Stuart
 
Very strange behavior, first of all; the configuration file kicked off successfully from within the main shell script, but the value of the key set in the configuration file did not take. I tried getting to the root of the problem.

This is what I did:

1a) Executed the configuration file at the command prompt [./config.sh]
1b) echo $LOGS [No value to stdout]

2a) Manually entered, “LOGS=/usr/files” at the command prompt
2b) echo $LOGS [“/usr/files” echoed to stdout]

This is what I have in my configuration file [config.sh] :

#!/bin/ksh
LOGS=/usr/files

This is what I have in my main script [config.sh] :

#!/bin/ksh
# Call configuration file
./config.sh

# echo results to stdout
echo The location of the log file directory is, $LOGS


chmod is set to 777 on each file
 
You forgot to export the environment variable (LOGS), for example your config file should read:

#!/bin/ksh
export LOGS=/usr/files

I'm not that familiar with the Korn shell but if memory serves this is the right syntax for the export command. If not, then substitute the appropriate command to export the environment variable or check the man pages.

If you run the config file from the '/etc/profile' file which all users use before using their own $HOME/.profile file, then you wouldn't have to run the config file in each and every script. Alternatively, you could just put the variables themselves (LOGS) in /etc/profile instead of running the config file in it, and every user would see it just the same.

Hope this helps, and good luck.

- Stuart
 
I forgot to mention, I had used the export command on one of my tries, I just happened to show the example without me using it. The Korn shell does utilize the export command.

I would like to use the configuration file, in a similar manner to how UNIX programs use ".conf files". However UNIX programs call a ".conf file", I would like for my scripts to call my configuration file in a similar manner. Ultimately, I would like to use these scripts in a different environment without having to change the /etc/profile file every time I drop in my script.
 
Interesting thread ... I have done something similar recently, where I have a .cfg file where I set values/paths, etc. I then use awk in my scripts to obtain these values. For example, I have an ftp script which takes the name of the remote server from a cfg file. The .cfg file contains the following lines (there are other settings there but this is an example).
[tt]
# config file for ftp utility
SERVER=myserver
[/tt]
Then, in my ftp script, to get the server name, all I need to do is include the lines
[tt]
# set location of .cfg file to variable
CFG=/path/to/file/ftp.cfg
# use awk to extract appropriate value
SERVERNAME=`awk 'BEGIN{FS="="} $1=="SERVER" {print $2}' $CFG`
[/tt]

Hope this helps a bit.

Greg.
 
Interesting option grega, although a bit more involved. Each program extracts the appropriate tag(s) from a single .cfg file for their use. I will have to try this out for myself.

- Stuart
 
Thanks grega, this is what I wanted.

This sounds like a good option, true its a little more involved but I think its for the better.
 
The reason that your enviornment variables are not taking has to do with the shell. You need to source your config file within the current shell. In korn shell you would execute from the command line

. ./config_file_name

This will make the variables available to your current shell. If your were using cshell
it would be
source ./config_file_name

The period is the equilivant of the source command in korn shell. I think this will solve your problem.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top