SHELL中SED如何获取变量的值???
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SHELL中SED如何获取变量的值???相关的知识,希望对你有一定的参考价值。
这是我的想法,但是感觉不太对 目的是通过md5生成一条密码,然后放到grub.conf的第13行去
sed 在13行后面追加内容简单,
sed -i '13 a\\123456789' grub.conf前提是13行有内容才能成功
你试了一下你的命令,AAA变量没有内容啊,如果想用变量就把单引号变成双引号试试
参考技术A 用双引号就可以了, sed "/13/a $AAA"。单引号不转义引号内的任何字符,所有字符都取字面值。
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
以上是关于SHELL中SED如何获取变量的值???的主要内容,如果未能解决你的问题,请参考以下文章