Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

solaris ksh read variables from config file

Posted on 2011-09-22
4
Medium Priority
?
834 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

810 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