Shell parsing my.cnf configuration file

Source: Internet
Author: User

MY.CNF configuration format is as follows

VI my.cnf
[Client]
port=3306
Socket=/tmp/mysql.socket

[Mysqld]
port=3306
Server-id=1
Datadir=/usr/local/mysql/data

[Mysqld_safe]
port=3306

More parsemy.sh

#!/bin/bash
cnf=$ (CD ' DirName;p wd) "/MY.CNF"

#得到区块数组
g_sec= (' sed-n '/\[*\]/p ' $cnf |grep-v ' ^# ' |tr-d [] ')
#sed-n '/\[*\]/p ' to get rows containing [*]
#grep-V ' ^# ' removes the line from the #
#tr-D [] remove []
#g_sec = (client mysqld mysqld_safe)

For ((i=0;i<${#g_sec [@]};i++))
Do
echo "Parse No." $i
Sec_name=${g_sec[i]}
g_names= (' sed-n '/\[' $sec _name ' \]/,/\[/p ' $cnf |grep-ev ' \[|\]|^$|^# ' |awk-f ' = ' {print '} ')
#sed-n '/\[' $sec _name ' \]/,/\[/p ' Get all lines from [$sec _name] to near [
#grep-ev ' \[|\]|^$|^# ' remove rows containing [or] removing empty lines minus #
#awk-F ' = ' {print '} ' gets the = number preceding character

g_values= (' sed-n '/\[' $sec _name ' \]/,/\[/p ' $cnf |grep-ev ' \[|\]|^$|^# ' |awk-f ' = ' {print $} ')
#awk-F ' = ' {print '} ' get = sign after character

For ((j=0;j<${#g_names [@]};j++))
Do
echo ${g_names[$j]} "${g_values[$j]}
Done

Done

Shell parsing my.cnf 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.