sed在替换的时候,使用变量中的值?如何在sed实现变量的替换?获取到变量中的值?...
生活随笔
收集整理的這篇文章主要介紹了
sed在替换的时候,使用变量中的值?如何在sed实现变量的替换?获取到变量中的值?...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
需求描述:
今天在做nrpe配置的時候,想要通過批量的方式來將定義文件中的IP給替換掉
開始做的時候沒有成功,報錯了.在此記錄下,如何實現,獲取到變量的值,然后
進行替換.
操作過程:
1.原文件的內容
[root@testvm02 hosts]# cat 192.168.53.26.cfgdefine 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/bashfor i in `cat hostsip.dat` docd /usr/local/nagios/etc/objects/hostsecho "current ipaddress $i"cp 192.168.53.26.cfg $i.cfgsed -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.283.執行腳本,發現有如下報錯
[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: GPLWebsite: 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 hostsChecked 0 service dependenciesChecked 0 host dependenciesChecked 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 validdirectives and data definitions. If you are upgrading from a previousversion of Nagios, you should be aware that some variables/definitionsmay have been removed or modified in this version. Make sure to readthe 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-serverhost_name $i #發現這些的變量值都沒有獲取到.address $i }define service {use generic-service,graphed-service ; Name of service template to usehost_name $iservice_description System_Loadcheck_command check_nrpe!check_load }define service {use generic-service,graphed-servicehost_name $iservice_description disk_usagecheck_command check_nrpe!check_disk }5.所以修改腳本為以下
#!/bin/bashfor i in `cat hostsip.dat` docd /usr/local/nagios/etc/objects/hostsecho "current ipaddress $i"cp 192.168.53.26.cfg $i.cfgsed -i 's/192.168.53.26/'$i'/g' $i.cfg #增加單引號,表示通過$i獲取變量的值,然后進行替換. done echo 'De@2018er' | sudo -S service nagios restart6.重新執行腳本
[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-serverhost_name 192.168.53.28address 192.168.53.28 }define service {use generic-service,graphed-service ; Name of service template to usehost_name 192.168.53.28service_description System_Loadcheck_command check_nrpe!check_load }define service {use generic-service,graphed-servicehost_name 192.168.53.28service_description disk_usagecheck_command check_nrpe!check_disk }備注:變量已經替換成功,獲取到了變量的值,然后用這個變量的值進行了替換.
?
文檔創建時間:2018年8月2日19:05:10
轉載于:https://www.cnblogs.com/chuanzhang053/p/9409269.html
總結
以上是生活随笔為你收集整理的sed在替换的时候,使用变量中的值?如何在sed实现变量的替换?获取到变量中的值?...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常用binlog日志操作命令
- 下一篇: 关于props的注意事项!