sed在替换的时候,使用变量中的值?如何在sed实现变量的替换?获取到变量中的值?

需求描述:

  今天在做nrpe配置的时候,想要通过批量的方式来将定义文件中的IP给替换掉

  开始做的时候没有成功,报错了.在此记录下,如何实现,获取到变量的值,然后

  进行替换.

操作过程:

1.原文件的内容

[root@testvm02 hosts]# cat 192.168.53.26.cfg 
define host {

    use                     linux-server
    host_name               192.168.53.26
    address                 192.168.53.26
}

define service {

    use                     generic-service,graphed-service           ; Name of service template to use
    host_name               192.168.53.26
    service_description     System_Load
    check_command           check_nrpe!check_load
}

define service {
    use                 generic-service,graphed-service
    host_name           192.168.53.26
    service_description disk_usage
    check_command       check_nrpe!check_disk
}

2.想要拷贝出以其他的ip开头的文件,并且将其中的IP给替换掉,写了下面的脚本

#!/bin/bash

for i in `cat hostsip.dat`
do
    cd /usr/local/nagios/etc/objects/hosts
    echo "current ipaddress $i"
    cp 192.168.53.26.cfg $i.cfg
    sed -i 's/192.168.53.26/$i/g' $i.cfg
done
echo 'De@2018er' | sudo -S  service nagios restart

备注:通过hostsip.dat中获取到ip,然后拷贝文件,替换其中的内容

hostsip.dat文件中内容:

[root@testvm02 tmp]# cat hostsip.dat 
192.168.53.28

3.执行脚本,发现有如下报错

[root@testvm02 tmp]# sh config.sh 
current ipaddress 192.168.53.28
Running configuration check... 
Nagios Core 4.4.1
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2018-06-25
License: GPL

Website: https://www.nagios.org
Reading configuration data...
   Read main config file okay...
   Read object config files okay...

Running pre-flight check on configuration data...

Checking objects...
    Checked 12 services.
Error: The name of host '$i' contains one or more illegal characters.
    Checked 3 hosts.
    Checked 1 host groups.
    Checked 0 service groups.
    Checked 1 contacts.
    Checked 1 contact groups.
    Checked 26 commands.
    Checked 5 time periods.
    Checked 0 host escalations.
    Checked 0 service escalations.
Checking for circular paths...
    Checked 3 hosts
    Checked 0 service dependencies
    Checked 0 host dependencies
    Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   1

***> One or more problems was encountered while running the pre-flight check...

     Check your configuration file(s) to ensure that they contain valid
     directives and data definitions.  If you are upgrading from a previous
     version of Nagios, you should be aware that some variables/definitions
     may have been removed or modified in this version.  Make sure to read
     the HTML documentation regarding the config files, as well as the
     'Whats New' section to find out what has changed.

备注:通过以上的内容,可以看到,是cfg配置错误.

4.查看新建的cfg配置

[root@testvm02 hosts]# cat 192.168.53.28.cfg 
define host {

    use                     linux-server
    host_name               $i     #发现这些的变量值都没有获取到.
    address                 $i
}

define service {

    use                     generic-service,graphed-service           ; Name of service template to use
    host_name               $i
    service_description     System_Load
    check_command           check_nrpe!check_load
}

define service {
    use                 generic-service,graphed-service
    host_name           $i
    service_description disk_usage
    check_command       check_nrpe!check_disk
}

5.所以修改脚本为以下

#!/bin/bash

for i in `cat hostsip.dat`
do
    cd /usr/local/nagios/etc/objects/hosts
    echo "current ipaddress $i"
    cp 192.168.53.26.cfg $i.cfg
    sed -i 's/192.168.53.26/'$i'/g' $i.cfg    #增加单引号,表示通过$i获取变量的值,然后进行替换.
done
echo 'De@2018er' | sudo -S  service nagios restart

6.重新执行脚本

[root@testvm02 tmp]# sh config.sh 
current ipaddress 192.168.53.28
Running configuration check... Stopping nagios: .done.
Starting nagios: Running configuration check... done.

7.查看新生成的文件

[root@testvm02 hosts]# cat 192.168.53.28.cfg 
define host {

    use                     linux-server
    host_name               192.168.53.28
    address                 192.168.53.28
}

define service {

    use                     generic-service,graphed-service           ; Name of service template to use
    host_name               192.168.53.28
    service_description     System_Load
    check_command           check_nrpe!check_load
}

define service {
    use                 generic-service,graphed-service
    host_name           192.168.53.28
    service_description disk_usage
    check_command       check_nrpe!check_disk
}

备注:变量已经替换成功,获取到了变量的值,然后用这个变量的值进行了替换.

 

文档创建时间:2018年8月2日19:05:10

posted @ 2018-08-02 19:06  Zhai_David  阅读(2789)  评论(0编辑  收藏  举报