LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-03-2009, 10:09 AM   #1
carters2
LQ Newbie
 
Registered: Sep 2009
Posts: 20

Rep: Reputation: 1
Shell script that reads from parameters file


Ok here is what I am trying to do but I am not sure exactly how to do it. I want to write a shell script that will replace certain values in a file with environment specific information that it pulls from a parameters file. The paramaters file looks like the following...

[dev]
ip=10.15.109.41
name=dev1.mydomain.com
port=7001
[/dev]

[int]
ip=10.15.109.42
name=int1.mydomain.com
port=7002
[/int]

[qa1]
ip=10.15.109.43
name=qa1.mydomain.com
port=7003
[/qa1]

[qa2]
ip=10.15.109.44
name=qa2.mydomain.com
port=7004
[/qa2]

Now the shell script I need to write should take two parameters when you call it

./myscript.sh <file_location> <dev|int|qa1|qa2>

So if I call the script with the second parameter of "int" it will read all of the parameters for INT so I can use them with sed to replace lines of a config file.
 
Old 11-03-2009, 07:14 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Show what you've got so far. You may want to refer to these:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 11-03-2009, 11:38 PM   #3
bobgosling
LQ Newbie
 
Registered: Jul 2009
Location: Singapore
Posts: 6

Rep: Reputation: 1
Why ?

Is there any pressing reason why your parameter file is structured that way ?

If it is possible to restructure it like this you will find it much easier to do what you want by simply grepping the appropriate lines :

dev.ip=10.15.109.41
dev.name=dev1.mydomain.com
dev.port=7001

int.ip=10.15.109.42
int.name=int1.mydomain.com
int.port=7002

qa1.ip=10.15.109.43
qa1.name=qa1.mydomain.com
qa1.port=7003

etc

( Better still , use something other than dot as a delimiter between key and value )

If you are stuck with your structure then you need to read the file line-by line until you find the start of your required block then read until you find the end of the block which is going to require some inefficient code using loops.

An awk guru may be able to tell you how to extract the required block using awk , but I'm afraid I'm too busy at the moment to look that up.
 
Old 11-04-2009, 01:23 AM   #4
bobgosling
LQ Newbie
 
Registered: Jul 2009
Location: Singapore
Posts: 6

Rep: Reputation: 1
this works

#!/bin/sh
# in real life some parameter checking would go here
export paramfile="$1"
export start_tag="\[$2\]"
export end_tag="\/$2\]"
export block_found="false"
for line in `cat ${paramfile}`
do
# check for start of block
if [ "${block_found}" != "true" -a "`echo ${line} | grep ${start_tag} | grep -v grep | wc -l`" == "1" ]
then
export block_found="true"
fi
# check for end of block
if [ "${block_found}" == "true" -a "`echo ${line} | grep ${end_tag} | grep -v grep | wc -l`" == "1" ]
then
export block_found="false"
fi
# process block ( unless this is the start_tag line )
if [ "${block_found}" == "true" -a "`echo ${line} | grep ${start_tag} | grep -v grep | wc -l`" != "1" ]
then
export key="`echo ${line} | sed 's/=/ /' | awk '{print $1}'`"
export val="`echo ${line} | sed 's/=/ /' | awk '{print $2}'`"
export cmd="export ${key}=\"${val}\""
eval ${cmd}
fi
done
echo "ip = ${ip}"
echo "name = ${name}"
echo "port = ${port}"
exit
 
Old 11-04-2009, 09:24 AM   #5
carters2
LQ Newbie
 
Registered: Sep 2009
Posts: 20

Original Poster
Rep: Reputation: 1
Wow! That is awesome. Thanks a lot that is a huge help
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script for recording cpu%, concurrent user and other parameters irfanb146 Linux - General 5 03-17-2008 10:38 PM
shell script (sending parameters to scripts) qrshat Programming 4 12-05-2007 01:15 PM
Passing parameters to a shell script neocookie Linux - General 5 10-20-2005 12:44 PM
passing parameters to functions in shell script kushalkoolwal Programming 1 09-28-2005 03:40 PM
How to create a shell script that reads something from stdin? ricky_ds Programming 2 05-17-2005 06:06 AM


All times are GMT -5. The time now is 09:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration