Solved

solaris ksh read variables from config file

Posted on 2011-09-22
4
795 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
wordappend challenge 8 225
Remote install of MSI file 4 54
Unable to disassociate and associate elastic IP address EC2-VPC 6 53
x-bar in Google Sheets 2 97
Utilizing an array to gracefully append to a list of EmailAddresses
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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…

730 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