First, simple version
Refer to the example of StackOverflow and change one out:
Copy Code code as follows:
While ifs= ' = ' Read var val
Todo
if [[$var = = \[*]]]
Then
section=$ (echo $var | sed ' s/^\[\ (. *\) \]$/\1/')
elif [[$val]]
Then
If [-Z $section];then
Declare "${var}= $val"
Else
Declare "${section}.${var}= $val"
Fi
Fi
Done < Config.ini
When in use:
Copy Code code as follows:
You can read the variables.
Second, the complex version
Copy Code code as follows:
[Comon]
Ids=com1,com2,com3
Files=profilefile
[COM1]
key= "Name"
file= "Test"
[COM2]
key= "Name1"
file= "Test"
[COM3]
key= "Name2"
file= "Test"
Take [COM1] key value I checked, fortunately in the Chinaunix inside found the order (seemingly a call Wintty brother wrote):
Copy Code code as follows:
Awk-f ' = '/\[com1\]/{a=1}a==1&&$1~/key/{print $2;exit} ' Config.ini
This will simply fetch the value.
Check the order. Finally understand, the order is divided into two parts:
Pattern matching to: [COM1] then execute the action: A=1, followed by a pattern + command
Mode: "a==1&&$1~/key/"
A==1 because the value is already assigned, the next step is to execute if the first field matches the value of the key.
Print item 2nd, then exit, and exit will not print to a key value that matches [COM2] and [COM3].
The requirements will become, now become the value of the "COM" several keys, the shell provided by the script is as follows:
Copy Code code as follows:
#! /bin/sh
GetConfig ()
{
Section=$1
Confile=$2
Endprint= "Key\tfile\t"
echo "$ENDPRINT"
For loop in ' echo $ENDPRINT |tr ' \ t '
Todo
#这里面的的SECTION的变量需要先用双引号, and then in single quotes, I think I can understand that
#单引号标示是awk里面的常量, because $ is a special character of a regular expression, double quotes, a value that marks the variable
#{gsub (/[[:blank:]]*/, "", $) remove whitespace content on both sides of the value
Awk-f ' = '/\[' "$SECTION" ' \]/{a=1}a==1&&$1~/' "$loop" '/{gsub (/[[:blank:]]*/, "", $);p rintf ("%s\t", $); exit } ' $CONFILE
Done
}
#更改变量名称
Configfile=$1
echo "========================================================"
#文件名称
echo "+++configname: $CONFIGFILE +++++++++++++++++++++++++++++++"
#取得ids中的每个id把, divide the number into spaces because the loop content is separated by a space
Profile= ' sed-n '/ids/' P $CONFIGFILE | Awk-f= ' {print $} ' | Sed ' s/,//g '
#对于一个配置文件中的所有id循环
For onecom in $profile
Todo
echo "--------------------------------------------------"
echo "COM: $OneCom"
#此处函数调用有时候不能用反引号, or there will be mistakes, this is not clear to know the trouble please tell
GetConfig $OneCom $CONFIGFILE
echo "\ n"
echo "--------------------------------------------------"
#break
Done
echo "========================================================"
The results of the implementation are as follows:
Copy Code code as follows:
$ one.sh File
========================================================
+++configname:file+++++++++++++++++++++++++++++++
--------------------------------------------------
Com:com1
Key file
Name File1
--------------------------------------------------
--------------------------------------------------
Com:com2
Key file
name1 File2
--------------------------------------------------
--------------------------------------------------
Com:com3
Key file
Name2 File3
--------------------------------------------------
Hey, the demand has changed, the configuration file has multiple, how to take multiple files of the configuration items:
Copy Code code as follows:
#! /bin/sh
GetConfig ()
{
Section=$1
Confile=$2
Endprint= "Key\tfile\t"
echo "$ENDPRINT"
For loop in ' echo $ENDPRINT |tr ' \ t '
Todo
Awk-f ' = '/\[' "$SECTION" ' \]/{a=1}a==1&&$1~/' "$loop" '/{gsub (/[[:blank:]]*/, "", $);p rintf ("%s\t", $); exit } ' $CONFILE
Done
}
#显示的多个文件名将多行的回车符转成逗号分隔符
configfiles= ' ls $1|tr ' \ n ', '
#查看到底有多个配置文件
_num= ' echo $CONFIGFILES |tr-cd \,|wc-c '
#临时变量保存配置多个文件
_tmpfiles= $CONFIGFILES
While [$_num-ge 1]
Todo
#得到一个文件
Configfile= ' echo $_tmpfiles|cut-d ', '-f1 '
#余下的文件
_tmpfiles= ' echo $_tmpfiles|cut-d ', '-f2-'
#配置文件数量减一
_num=$ (($_num-1))
echo "========================================================"
#文件名称
echo "+++configname: $CONFIGFILE +++++++++++++++++++++++++++++++"
Profile= ' sed-n '/ids/' P $CONFIGFILE | Awk-f= ' {print $} ' | Sed ' s/,//g '
#对于一个配置文件中的所有id循环
For onecom in $profile
Todo
echo "--------------------------------------------------"
echo "COM: $OneCom"
GetConfig $OneCom $CONFIGFILE
echo "\ n"
echo "--------------------------------------------------"
#break
Done
echo "========================================================"
Done
Two hours finally written, please reprint the time do not forget to add my address oh, also do not waste my hard work.