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

需求描述:

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

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

  进行替换.

操作过程:

1.原文件的内容

[[email protected] 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 ‘[email protected]‘ | sudo -S  service nagios restart

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

hostsip.dat文件中内容:

[[email protected] tmp]# cat hostsip.dat
192.168.53.28

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

[[email protected] 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配置

[[email protected] 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 ‘[email protected]‘ | sudo -S  service nagios restart

6.重新执行脚本

[[email protected] tmp]# sh config.sh
current ipaddress 192.168.53.28
Running configuration check... Stopping nagios: .done.
Starting nagios: Running configuration check... done.

7.查看新生成的文件

[[email protected] 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

原文地址:https://www.cnblogs.com/chuanzhang053/p/9409269.html

时间: 2024-07-31 22:42:12

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

thinkphp5.0学习笔记(三)获取信息,变量,绑定参数

1.构造函数: 控制器类必须继承了\think\Controller类,才能使用: 方法_initialize 代码: <?php namespace app\lian\controller; use think\Controller; use think\Db; use think\Request; class Index extends Controller { public function _initialize() { echo 'init|||'; } public function

class_copyIvarList方法获取实例变量问题引发的思考

在runtime.h中,你可以通过其中的class_copyIvarList方法来获取实例变量.具体的实现如下(记得导入头文件<objc/runtime.h>): - (NSArray *)ivarArray:(Class)cls { unsigned int stuIvarCount = 0; Ivar *ivars = class_copyIvarList(cls, &stuIvarCount); if (stuIvarCount == 0) { return nil; } NSM

centos 7中的文本处理工具sed命令的使用

sed是一个强大而简答的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除.替换.添加.移动等),最后输出所有行或者仅输出处理的某些行.sed也可以在无交互的情况下实现相当复杂的文本处理操作,被广泛应用于shell脚本中,用以完成各种自动化处理任务.sed的工作流程主要包括读取.执行和显示三个过程: 读取:sed从输入流(文件.管道.标准输入)中读取一行内容并存储到临时的缓冲区中(也称为模式空间). 执行:默认情况下,所有的sed命令都在模式空间中顺序地执行,除非指定了行的

Linux中的shell脚本编程——数组 ,高级字符串操作 ,高级变量 ,配置用户环境

概述: 概述: 本章节将讲述Linux中shell脚本编程的数组,高级字符串处理,高级变量和配置用户的环境. 一.函数和位置参数扩展 1.shift 命令实现在位置参数中的跳跃,会把最左边的参数挤掉     2.匿名函数的使用 第一篇:数组 一.数组的定义: 1.变量:存储单个元素的内存空间 2.数组:存储多个元素的连续的内存空间,相当于多个变量的集合. 3.数组名和索引 索引:编号从0开始,属于数值索引 注意:索引可支持使用自定义的格式,而不仅是数值格式,即为关联索引,bash4.0版本之后开

java中获取环境变量

分为获取java自身的一些环境变量和和操作系统相关的环境变量. 获取JVM相关的一些变量 在运行时设置一个环境变量 debug 为 true: java -Ddebug=true YourClass在程序中设置一个环境变量 debug 为 true: System.setProperty( "debug", "true" );获取一个环境变量 debug : String debug = System.getProperty( "debug" )

获取ACCESS数据库中自增变量的值

/// <summary> /// 获取自增变量值 /// </summary> /// <returns>自增变量值</returns> public int getReturnIdentity() { //Conn_Open(); string strCMDtext = @"select @@identity"; OleDbCommand cmd_sql = new OleDbCommand(strCMDtext, conn_1);

Python中敏感信息(用户名、密码等)处理方法——添加到环境变量

前言: 写python程序时,经常碰到如:发送邮件.配置账号信息.难免需要在python程序中明文填写敏感信息,这个时候可以将敏感信息写入系统环境变量,通过python自带os模块去获取变量. Linux下: [email protected]:~# export USERNAME=cctv [email protected]:~# [email protected]:~# python Python 2.7.3 (default, Mar 14 2014, 11:57:14) [GCC 4.7

js中,for循环里面放ajax,ajax访问不到变量以及每次循环获取不到数据问题总结

首先晒下自己代码 for ( var i = 0; i < rows.length; i++) { $.ajax({ type:'POST', url:'${ctx }/admin/store_item/findStoreItemsByType?typeid=' +flag+'&id='+rows[i].id,//请求的url地址 async: false,//设置成同步 dataType:'json', success:function(data){ if(flag==1){ $('#ch

PowerDesigner中在生成的数据库脚本中用name列替换comment列作为字段描述的方法

1 PowerDesigner中在生成的数据库脚本中用name列替换comment列作为字段描述的方法如下, 依次打开Tools -- Execute Commands -- Run Script,运行以下脚本: Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl 'the current model 'get the current active model Set mdl = ActiveModel