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.
Copyright (c) -present Nagios Core Development Team and Community Contributors
Copyright (c) - Ethan Galstad
Last Modified: --
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 services.
Error: The name of host '$i' contains one or more illegal characters.
Checked hosts.
Checked host groups.
Checked service groups.
Checked contacts.
Checked contact groups.
Checked commands.
Checked time periods.
Checked host escalations.
Checked service escalations.
Checking for circular paths...
Checked hosts
Checked service dependencies
Checked host dependencies
Checked timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings... Total Warnings:
Total Errors: ***> 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
sed在替换的时候,使用变量中的值?如何在sed实现变量的替换?获取到变量中的值?的更多相关文章
- Vue中父组件使用子组件的emit事件,获取emit事件传出的值并添加父组件额外的参数进行操作
需求是这样的,需要输入这样一个列表的数据,可以手动添加行,每一行中客户编号跟客户姓名是自动关联的,就是说选取了客户姓名之后,客户编号是自动填充的,客户姓名是一个独立的组件,每一个下拉项都是一个大的对象 ...
- java 获取系统变量(环境变量和设置变量)
前言 环境变量这个概念不陌生, 就是操作系统的环境变量. 系统变量就是java本身维护的变量. 通过 System.getProperty 的方式获取. 对于不同的操作系统来说, 环境变量的处理可能会 ...
- thinkphp5.0学习笔记(三)获取信息,变量,绑定参数
1.构造函数: 控制器类必须继承了\think\Controller类,才能使用: 方法_initialize 代码: <?php namespace app\lian\controller; ...
- class_copyIvarList方法获取实例变量问题引发的思考
在runtime.h中,你可以通过其中的一个方法来获取实例变量,那就是class_copyIvarList方法,具体的实现如下: - (NSArray *)ivarArray:(Class)cls { ...
- 项目中通过Sorlj获取索引库中的数据
在开发项目中通过使用Solr所提供的Solrj(java客户端)获取索引库中的数据,这才是真正对项目起实质性作用的功能,提升平台的检索性能及检索结果的精确性 第一步,引入相关依赖的jar包 第二步,根 ...
- docker-compose 布署应用nginx中的create-react-app应用获取环境变量
文章来源:https://www.freecodecamp.org/news/how-to-implement-runtime-environment-variables-with-create-re ...
- python raw String 获取字符串变量中的反斜杠
常用的获取raw string的方式为: >>>r'\n' \n 不能用在字符串变量中,获取字符串变量中的反斜杠如下: tab = '\n' >>>tab.enco ...
- C#中如何获取系统环境变量等
C#中获取系统环境变量需要用到Environment 类. 其中提供了有关当前环境和平台的信息以及操作它们的方法.该类不能被继承 以下代码得到%systemdrive%的值,即“C:” string ...
- PowerDesigner中在生成的数据库脚本中用name列替换comment列作为字段描述的方法
1 PowerDesigner中在生成的数据库脚本中用name列替换comment列作为字段描述的方法如下, 依次打开Tools -- Execute Commands -- Run Script,运 ...
随机推荐
- EF5.x Code First 一对多关联条件查询,Contains,Any,All
背景 通过多个部门id获取所有用户,部门和用户是多对多. 已知部门id,获取该部门包括该部门下的所有子部门的所有用户. 关系如下: public class Entity:IEntity { publ ...
- VS2013安装MVC5
打开VS 选择 .net 4.5 创建项目 右击项目 选择管理NuGet 输入Microsoft.AspNet.Mvc -Version 5.0.0 安装最新版本的MVC5
- 【jquery】jquery 实现许愿墙效果,可拖拽
最近整理做过的项目,发现有个许愿墙的效果,今天拿出来给大家分享下. 先简单的介绍下功能点: 随机显示背景图或背景色 出现的位置随机 可以通过拖拽改变位置 好了,附上代码: <!doctype h ...
- Visual Studio 2008 调试运行Bug记录
1.VS2008LINK : fatal error LNK1000: Internal error during IncrBuildImage (1). 打开要编译的项目(2). 在项目菜单中打开属 ...
- jquery跨域访问解决方案
客户端“跨域访问”一直是一个头疼的问题,好在有jQuery帮忙,从jQuery-1.2以后跨域问题便迎刃而解.由于自己在项目中遇到跨域问题,借此机会对跨域问题来刨根问底,查阅了相关资料和自己的实践,算 ...
- Ubuntu16.04安装Mininet
Ubuntu16.04源码安装Mininet 本文介绍了VMware虚拟机ubuntu16.04中安装Mininet的方法,物理机中的方法与之相同.主要参考了Mininet官方的教程.官方提供了四种安 ...
- USB学习笔记连载(七):CY7C68013A 无法识别的可能原因
最近一直在调试视频 采集卡,和PC端连接的是USB接口,使用的是cypress的CY7C68013A-56PVXC. //======================================= ...
- 安装GYP(Generate Your Projects)
GYP(Generate Your Projects)是由 Chromium 团队开发的跨平台自动化项目构建工具,Chromium 便是通过 GYP 进行项目构建管理. 主页 :http://code ...
- c递归程序
递归 long recursin(int); void main() { int n; long result; printf("input a integer number: \n&quo ...
- BitSet 是个好东西
顾名思义,就是位集合(bit set),是从JDK 1.0就出现的东西,后面的版本又慢慢强化. 我们说学习一样东西,最好是场景驱动 - 要考虑它的使用场景,这样才有意义. 那么,BitSet的应用场景 ...