Shell implementation read INI format config file method _linux Shell

Source: Internet
Author: User

INI file format is generally composed of sections, keys, values of three parts

Format:

[Section I.]
First key = value
Second key = second value
[Section II]
First key = Val1,val2,val3
Example:

Copy Code code as follows:

[COM]
Kinggoo = Jb51.net

In fact, this role is not so special, but multiple shell if each configuration information is written in the shell file, understand the people are OK, half understand + not careful how to change, to change a few local estimate script can not run. So it's better to be alone, and it can be configured with multiple shells using an INI file (regardless of the extension). You just need to add a custom function to the shell.
Copy Code code as follows:

# __readini [Profile path + name] [node naming] [key value]
function __readini () {
inifile=$1; section=$2; Item=$3
_readini= ' awk-f ' = '/\[' $SECTION ' \]/{a=1}a==1&&$1~/' $ITEM '/{print $2;exit} ' $INIFILE '
Echo ${_readini}
}

How to use:
Copy Code code as follows:

__readini File.ini Section KEY

Example: There is an INI format file Kinggoo.ini, which reads as follows
Copy Code code as follows:

[IP]
IP = jb51.net

[Maillist]
Mail = admin@jb51.net


Script File Contents:
Copy Code code as follows:

#!/bin/bash
# __readini [Profile path + name] [node naming] [key value]
function __readini () {
inifile=$1; section=$2; Item=$3
_readini= ' awk-f ' = '/\[' $SECTION ' \]/{a=1}a==1&&$1~/' $ITEM '/{print $2;exit} ' $INIFILE '
Echo ${_readini}
}
# Use Method:
# Net Address
_ip= ($ (__readini kinggoo.ini IP))
Echo ${_ip}
# Mail Send
_maillist= ($ (__readini kinggoo.ini maillist Mail))
Echo ${_maillist}

The Execute script file can either

When you use it, verify that you use it later.

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.