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. |
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/ |
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. |
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 |
Wow! That is awesome. Thanks a lot that is a huge help
|
All times are GMT -5. The time now is 08:33 AM. |