[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

solaris ksh read variables from config file

Posted on 2011-09-22
4
Medium Priority
?
825 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
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

656 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