?
Solved

solaris ksh read variables from config file

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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.
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

771 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