如何用SED批量 查找、添加、删除、替换配置文件里面的选项

废话不说,直接上代码,注意里面的单引号、双引号及转义符:

#!/bin/sh
function mariadb_conf {
        #yum install mariadb mariadb-server MySQL-python
        #cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
        #mysql conf
        if grep -q default-storage-engine /etc/my.cnf;then
                echo "ok for mysql conf already ok"
        else
                #\cp conf/my.cnf /etc/my.cnf
                \cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
                str[1]="bind-address = 10.0.0.11"
                str[2]="default-storage-engine = innodb"
                str[3]="innodb_file_per_table"
                str[4]="collation-server = utf8_general_ci"
                str[5]="init-connect = 'SET NAMES utf8'"
                str[6]="character-set-server = utf8"
                line=`grep -n "\[mysqld\]" /etc/my.cnf | cut -d ":" -f 1`
                for i in `seq 6`;do
                        sed -i ''$line'a'"${str[$i]}" /etc/my.cnf
                done
                systemctl enable mariadb.service
                systemctl restart mariadb.service

                #/etc/init.d/mysqld start
                mysqladmin -u root password openstack
                echo "fix mysql conf successful"
        fi
}

function keystone {
        #install  keystone
        ###cteate radom 10 num for keystone
        #ADMIN_TOKEN=`openssl rand -hex 10`
        #mkdir /etc/keystone
        #mkdir /var/log/keystone
        #mkdir /var/run/keystone

        pid=`ps -e | grep keystone-all | grep -v grep | wc -l`
        if [ $pid -gt 0 ];then
                echo "ok for keystone"
        else
                #line=`grep -n "#admin_token = ADMIN" $conf | cut -d ":" -f 1`
                #sed -i ''$line'a'"admin_token = 1a267aae6d394e2f97b0" $conf
                ###test conf
                ###grep "^[a-z]" /etc/keystone/keystone.conf
                #\cp ../conf/keystone.conf /etc/keystone/
                conf=/etc/keystone/keystone.conf

                strs[1]="#admin_token = ADMIN"
                strd[1]="admin_token = 1a267aae6d394e2f97b0"

                strs[2]="#debug = false"
                strd[2]="debug = true"

                strs[3]="#verbose = false"
                strd[3]="verbose = true"

                strs[4]="#log_file = <None>"
                strd[4]="log_file = keystone.log"

                strs[5]="#log_dir = <None>"
                strd[5]="log_dir = \/var\/log\/keystone"

                strs[6]="#connection = <None>"
                strd[6]="connection = mysql:\/\/keystone:keystone\@controller1\/keystone"

                strs[7]="#provider = keystone.token.providers.uuid.Provider"
                strd[7]="provider = keystone.token.providers.uuid.Provider"

                strs[8]="#driver = keystone.token.persistence.backends.sql.Token"
                strd[8]="driver = keystone.token.persistence.backends.sql.Token"

                for i in `seq 8`;do

                        sed -i '/'"^${strd[$i]}"'/d'   $conf
                        line=`grep -n "${strs[$i]}"    $conf | cut -d ":" -f 1`
                        #echo $line
                        sed -i ''$line'a'"${strd[$i]}" $conf
                done

                keystone-manage pki_setup --keystone-user root --keystone-group root
                chown -R keystone:keystone /var/log/keystone
                chown -R keystone:keystone /etc/keystone/ssl
                chown -R keystone:keystone /etc/keystone
                chmod -R o-rwx /etc/keystone/ssl
                #Populate the Identity service database:
                #keystone-manage db_sync
                su -s /bin/sh -c "keystone-manage db_sync" keystone
                systemctl enable openstack-keystone.service
                systemctl restart openstack-keystone.service
        fi

        #add crond
        #(crontab -l -u keystone 2>&1 | grep -q token_flush) || \
        #echo '@hourly /usr/bin/keystone-manage token_flush >/var/log/keystone/keystone-tokenflush.log 2&1' \
        #>> /var/spool/cron/keystone
}
mariadb_conf
keystone
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值