sed在替换的时候,使用变量中的值?如何在sed实现变量的替换?获取到变量中的值?
变量 实现 如何 获取 替换 时候 sed 使用
2023-09-11 14:14:16 时间
需求描述:
今天在做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
相关文章
- C#.NET常见问题(FAQ)-如何使用变量动态添加控件
- Android For JNI(三)——C的指针,指针变量,指针常见错误,值传递,引用传递,返回多个值
- python中将函数赋值给变量时需要注意的一些问题
- 深入理解Python变量与常量
- DISPLAY变量和xhost(原创)
- Atitit 提升语法级别4gl 4.5g 4.9g 5g 目录 1. 语言级别表 1 2. 4.9g实现细节 2 2.1. $dollor前导符 2 2.2. Static变量 2 2.3. S
- atitit.复合变量,也就是类似$$a的变量的原理与实现 java c#.net php js
- Angular export class AppComponent里定义变量的实现原理
- Python语言编程学习:文件路径变量修改,利用os模块固定文件父路径,变换文件子路径实现代码
- 基于有序模式的度量对多变量时间序列进行非线性分析(Matlab代码实现)
- 基于人工大猩猩部队优化CNN-LSTM(GTO-CNN-LSTM)多变量时间序列预测(Matlab代码实现)
- 习题 9.1 定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。
- 习题6.13 定义一个Student类,包含如下内容:成员变量:学号,姓名,性别,班干部否,数学,语文,外语 成员方法:输入,总分,平均分 编程实现这个类,并调用相应的方法输入数据,计算总分和平均分。
- VB编程:Static 静态变量实现计数功能实例-1
- ThreadLocal详解(实现多线程同步访问变量)
- 条件变量
- c# 变量,对象,静态类型,集合类的线程安全回顾
- LabVIEW 设定NI-DAQmx通道的共享变量的轮询速率
- 用结构体变量的引用作函数参数
- 利用引用形参实现两个变量的值互换
- Go语言编程:使用条件变量Cond和channel通道实现多个生产者和消费者模型
- ML之FE:数据预处理中基于pandas实现类别型字段数据编码(包括自定义编码映射字典)、目标变量布尔类型化且同时输出raw_df和df数据之代码实现攻略
- DataScience:风控场景之金融评分卡模型的数据探究EDA、预处理、变量选取、构建(逻辑回归)&开发(转评分卡)、使用过程(线上实现)之详细攻略
- python基础语法(第一章)(变量,注释,判断,循环,输入,输出)
- 特征工程--单时间变量特征
- Go语言入门-掌握go语言的变量和数据类型
- 【读书笔记】《Verilog数字系统设计教程》 第3章 模块的结构、数据类型、变量和基本运算符号(附思考题答案)