Solved

solaris ksh read variables from config file

Posted on 2011-09-22
4
779 Views
Last Modified: 2013-12-26
I would like to create a config file dbaccess.cnf with the contents
HOST=192.160.0.1
USER=dbuser
PASS=dbpass

I would like to be able to read the dbaccess.cnf file contents to create a config file for reading in to a variable in ksh to be placed into a mysql statement that will be used for other tasks.  I want to define the variables as
myhost = '<value received from dbaccess.cnf where line starts with "HOST=">'
echo $myhost
myuser = '<value received from dbaccess.cnf where line starts with "USER=">'
echo $myuser
etc

etc, for each of the values in the config file.  Not necessarily limited to the 3 I defined.

What I want is to a sample ksh script that will read the values from the dbaccess.cnf and the the value of the fields defined above.

I can probably take the rest of the scripting from that point.  I prefer not to use a temp file.

Thanks,
Brian
0
Comment
Question by:bray007
4 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36585647
Hi,

basically you would try commands like

myhost=$(awk -F= '/^HOST/ {split($2,A," "); print A[1]}' inputfile)
myuser=$(awk -F= '/^USER/ {split($2,A," "); print A[1]}' inputfile)

Please note that HOST, USER etc. must indeed start in column 1!
If this is not the case remove the caret ( ^ ).

Note also that the username, hostname etc. must be followed by at least one space (or end-of-line).
If this is not the case please let me know!

wmp
0
 
LVL 48

Expert Comment

by:Tintin
ID: 36589921
Why use different variable names?

Personally, I'd just do

#!/bin/ksh
. /path/to/dbaccess.dnf

echo $HOST
echo $USER

Open in new window



This way, you can add whatever settings you like in dbaccess.dnf and not change any of your code.
0
 
LVL 4

Accepted Solution

by:
Anacreo earned 500 total points
ID: 36712479
Tintin has a great solution...

But to do what you need you could do the following:
#!/bin/ksh

# This will not work if your parameters have spaces, but you could
# swap this out, with something like a read line, which I've done
# commented out, just use $line instead of $1 for same effect.
set - `cat vars`
# while read line; do
while [ $1 ]; do
 value=${1#*=}
 key=${1%=*}
 eval "my${key}=\$value"
shift
done
# done < vars

# Display the variables to confirm
set | grep ^my

Open in new window

0
 

Author Closing Comment

by:bray007
ID: 36904794
Thanks, this was the best solution that worked on the first try with my code.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now