[SOLVED] How to write a .sh script to read configuration file and write into .txt file
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to write a .sh script to read configuration file and write into .txt file
Okay so i'm very new to this (my first shell script actually) and having a hard time knowing even where to start..
so my task is to create a shell script that will read a configuration file, find the specific info I need from the config file and write it into a .txt file.
Here's an example of the config file:
file name = config.properties
Code:
#Tue Oct 18 16:23:06 GMT-05:00 2011
#The env property comes from the -D option -- devel/systest/preprod/prod
environment=${devel:env}
#Define properties that vary by environment here
ldapConfig.url=${${environment}.ldapConfig.url}
ldapConfig.password=${${environment}.ldapConfig.password}
ldapConfig.userDN=${${environment}.ldapConfig.userDN}
ldapPool.timeBetweenEvictionRunsMillis=${${environment}.ldapPool.timeBetweenEvictionRunsMillis}
ldapPool.maxActiveConnections=${${environment}.ldapPool.maxActiveConnections}
ldapPool.initialPoolSize=${${environment}.ldapPool.initialPoolSize}
ldapPool.minEvictableIdleTimeMillis=${${environment}.ldapPool.minEvictableIdleTimeMillis}
hazelcast.cluster.members=${${environment}.hazelcast.cluster.members}
#Environment specific properties
devel.ldapConfig.url=ldap\://155.165.192.12\:389
devel.ldapConfig.password=????
devel.ldapConfig.userDN=cn\=????,ou\=Users,ou\=Administration,o\=Cingular
devel.ldapPool.timeBetweenEvictionRunsMillis=1234556
devel.ldapPool.initialPoolSize=2
devel.ldapPool.maxActiveConnections=2
devel.ldapPool.minEvictableIdleTimeMillis=60000
What I need the script to do is go through the file and find info i need and paste into .txt file. This is an example of what I would like the .txt file to look like after i run the script on this config file:
So basically it should read the file, find the specific variables (or specific configurations) I specify and write them into a .txt file so I can attach it to my report.
look into the grep command particularly the -f argument.
Okay, so read the man page for that and still confused. Can you show me an example that I can work off of? Will I need to have a separate grep -f command for every variable I would like copied?
so my task is to create a shell script that will read a configuration file, find the specific info I need from the config file and write it into a .txt file.
unix unlike Microsoft dose NOT use file extensions
a text file is NOT defined by *.txt
a text file can be , well, anything
.csv ,.obj ,.sh ," no dot anything "
config files ARE text files
std input > std output
this sounds a bit like homework so an example of output
Code:
man grep > ~/man-grep-text
there is now a text file in your HOME folder
you can open that test file with any text editor ( kate, Gedit,Emacs,vi,nano)
I am a software tester and want to do this to make things easier to attach to my test report after each cycle. Since our configurations for each load scenario will be changing constantly, I need to keep track of the configurations per each load I run.
But anyways, this seems like a simple task to do, I understand the concept and what needs to be done just not exactly sure how to write the script so it will work properly.
Is there a way that I can write a script and insert lets say a list of configurations that need to be copied from the file? Where I can manually change the specific content it should look for..
Please read the manpage for the grep command. The -f option will read patterns to match from a file instead of from arguments. You could instead use multiple patterns but typing them after the -e option.
Also consider using sed. Especially if the order is important and you can't rely on it in the source config file.
If "devel" is not a constant, extract the value first, and then use in another grep or sed command to filter out the lines you want:
Code:
ENVTYPE=$(sed '/environment=\${/s/.*{\(.*\):env}/\1/')
sed -n "/${ENVTYPE}.ldapConfig.url=/p;/${ENVTYPE}.ldapPool.timeBetweenEvictionRunsMillis=/p" config.properties >config-seg.txt
I only included a couple lines. Simply add patterns to match. I used doublequotes to allow the $ENVTYPE variable to be evaluated by bash _before_ the sed command is run. You can also create a file of sed commands and have one command per line. Then the second sed command will be like:
Code:
sed -n -f filterrules.sed config.properties >config-seg.txt
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.