Shell script implementation to read the configuration file

Source: Internet
Author: User
Tags shebang

(I am participating in the 2015 blog Star Selection, invite you to vote, thank you: HTTP://VOTE.BLOG.CSDN.NET/BLOGSTAR2015/CANDIDATE?USERNAME=ZHOUZXI)

Recently, in the process of development work, a requirement was encountered: A shell script was used to read the configuration file and the Read configuration item was written to a new file.

To complete this requirement, we consider using the awk command, which reads the contents of the file line-by-row and processes each row of records according to a specific rule.

For example, we want to get the value of each configuration item from the file Config.ini (under the ETC directory of the current user) and write it to the NetworkInfo.txt file. Where the contents of the Config.ini file are as follows:

[network]; ip of databaseip=10.10.10.10; port of databaseport=9999

We write the following shell script getdatabaseconfig.sh:

#!/bin/kshcfg_file=$HOME/etc/config.iniip= ' awk' $1~/\[.*/{_cdr_par_=0} $ ~/^ *\[*network *\]/{_cdr_par_=1} $0~/^[\011]*ip *=.*/{if (_cdr_par_== 1) {sub ("=", ""); print $; Exit 0}} ' ${cfg_file}' Port= ' awk' $1~/\[.*/{_cdr_par_=0} $ ~/^ *\[*network *\]/{_cdr_par_=1} $0~/^[\011]*port *=.*/{if (_cdr_par _==1) {sub ("=", ""); print $; Exit 0}} ' ${cfg_file}`Main(){Echo "IP =" $IP    Echo "Port =" $PORT    Echo "[Network]">> NetworkInfo.txtEcho "ip=$IP">> NetworkInfo.txtEcho "port=$PORT">> NetworkInfo.txtEcho "Finish!!!!!!!!!!!"}# # Execute main functionMain $*

Upload the script to the Linux machine and use "chmod 777 getdatabaseconfig.sh" to modify the permissions of the script, and then run the "./getdatabaseconfig.sh" command with the following results:

ip10.10.10.10port9999finish!!!!!!!!!!!

At the same time, you can see that the NetworkInfo.txt file is generated in the current directory with the following contents:

[network]ip=10.10.10.10port=9999

The shell script in this article has been submitted to GitHub, welcome to download read: Https://github.com/zhouzxi/GetDatabaseConfig

Resources:
1.http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html
2.http://www.cnblogs.com/repository/archive/2011/05/13/2045927.html

Shell script implementation to read the configuration file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.