Create a shell to parse the INI file and use it to parse the ini configuration file in the HA framework.
The tested INI file is one found in the system.
# Example driver definitions# Driver from the postgresql-odbc package# Setup from the unixODBC package[PostgreSQL]Description= ODBC for PostgreSQLDriver= /usr/lib/psqlodbc.soSetup= /usr/lib/libodbcpsqlS.soDriver64= /usr/lib64/psqlodbc.soSetup64= /usr/lib64/libodbcpsqlS.soFileUsage= 1# Driver from the mysql-connector-odbc package# Setup from the unixODBC package[MySQL]Description= ODBC for MySQLDriver= /usr/lib/libmyodbc5.soSetup= /usr/lib/libodbcmyS.soDriver64= /usr/lib64/libmyodbc5.soSetup64= /usr/lib64/libodbcmyS.soFileUsage= 1
The iniparser. Sh script mainly checks whether the INI file is valid and the other is to read the value.
This script mainly provides these two functions for other scripts, and the processing will not be placed in the script. the last four lines are used for simple testing.
#! /Bin/bash # input parameter file name # Return Value 0, valid; other values are invalid or function check_syntax () {if [! -F $1]; then return 1 firet =$ (awk-F = 'in in {valid = 1} {# The row has been found invalid, if (valid = 0) Next # ignore empty rows if (length ($0) = 0) Next # Remove all spaces gsub ("| \ t ", "", $0) # Check whether the comment row head_char = substr ($, 1) if (head_char! = "#") {# Check whether the block name is in the form of field = value if (NF = 1) {B = substr ($, 1) len = length ($0) E = substr ($0, Len, 1) if (B! = "[" | E! = "]") {Valid = 0} else if (NF = 2) {# Check whether the start of field = value is [B = substr ($, 1) if (B = "[") {valid = 0} else {# multiple = numbers are not allowed. Valid = 0 }}end {print valid} '$1) if [$ ret-EQ 1]; thenreturn 0 elsereturn 2FI} # parameter 1 file name # parameter 2 block name # parameter 3 field name # Return 0, indicating correct, the output string indicates that the value of the corresponding field is found # Otherwise, the corresponding field is not found or the function get_field_value () {if [! -F $1] | [$ #-ne 3]; thenreturn 1 maid = $2 fieldname = $ 3begin_block = 0end_block = 0cat $1 | while read linedoif ["x $ line" = "X [$ blockname]"]; thenbegin_block = 1 continuefiif [$ begin_block-EQ 1]; thenend_block = $ (echo $ Line | awk 'begin {ret = 0}/^ \[. * \] $/{ret = 1} end {print RET} ') If [$ end_block-EQ 1]; then # echo "End Block" breakfineed_ignore = $ (echo $ Line | awk 'in in {ret = 0}/^ #/{ret = 1}/^ $/{ret = 1} End {print RET} ') If [$ need_ignore-EQ 1]; then # echo "ignored line: "$ linecontinuefifield = $ (echo $ Line | awk-F = '{gsub (" | \ t "," ", $1); print $1 }') value = $ (echo $ Line | awk-F = '{gsub ("| \ t", "", $2); print $2 }') # echo "'$ field': '$ value'" If ["x $ fieldname" = "x $ field"]; then # echo "result value: '$ result' "Echo $ valuebreak1_idonereturn 0} check_syntax odbcinst. iniecho "Check syntax status: $? "Global_field_value = $ (get_field_value odbcinst. ini PostgreSQL setup) echo" status: $ ?, Value: $ global_field_value"