Bash 在sed命令语法中调用变量
我正在创建一个脚本,使我的组织能够更轻松地将工作站添加到Nagios监控系统中 每次脚本运行时,我都希望能够输入主机名、别名和地址(作为用户输入的变量),并在sed命令中调用这些变量,以便“创建”一个新主机比键入所有内容快得多 我试图在sed找到后添加整个字符串 “主机的地址}” 我假设是/a(在找到所述字符串后追加) 尽管如此,使用双引号仍然不允许调用变量Bash 在sed命令语法中调用变量,bash,variables,sed,Bash,Variables,Sed,我正在创建一个脚本,使我的组织能够更轻松地将工作站添加到Nagios监控系统中 每次脚本运行时,我都希望能够输入主机名、别名和地址(作为用户输入的变量),并在sed命令中调用这些变量,以便“创建”一个新主机比键入所有内容快得多 我试图在sed找到后添加整个字符串 “主机的地址}” 我假设是/a(在找到所述字符串后追加) 尽管如此,使用双引号仍然不允许调用变量 #!bin/bash # Prompt for hostname, alias, and IP address of new host
#!bin/bash
# Prompt for hostname, alias, and IP address of new host
# take user input has variable to be used in sed
# Takes user input for hostname
echo host_name :
read var_hostname
# Takes user input for alias
echo alias :
read var_alias
# Takes user input for ip address
echo address :
read var_address
# searches for the top-most instance "address of the host" within the windows.cfg file
# after said instance is found, appends the new string below, which calls for the variables received
# previously by user
sed -i -e " address of the host } /a
define host{
use windows-server ; Inherit default values from a template
host_name $var_hostname ; The name we are giving to this host
alias $var_alias ; A longer name associated with the host
address $var_address ; IP address of the host
}"
windows.cfg
我希望字符串“define host{}”与用户输入的变量一起写入文件。改为修改windows.cfg的文件
cat windows.cfg
#some settings
#more settings
test
abc
lot of stuff
test2
backup
address of the host }
Sed脚本:
cat sed.sh
#!/bin/bash
# Prompt for hostname, alias, and IP address of new host
# take user input has variable to be used in sed
# Takes user input for hostname
echo host_name :
read var_hostname
# Takes user input for alias
echo alias :
read var_alias
# Takes user input for ip address
echo address :
read var_address
# searches for the top-most instance "address of the host" within the windows.cfg file
# after said instance is found, appends the new string below, which calls for the variables received
# previously by user
sed -e '/address of the host }/r'<(
echo "define host{"
echo " use windows-server ; Inherit default values from a template"
echo " host_name $var_hostname ; The name we are giving to this host"
echo " alias $var_alias ; A longer name associated with the host"
echo " address $var_address ; IP address of the host"
echo " }") -i -- windows.cfg
输出:
./sed.sh
host_name :
allan.com
alias :
allan
address :
123.123.123.123
cat windows.cfg
#some settings
#more settings
test
abc
lot of stuff
test2
backup
address of the host }
define host{
use windows-server ; Inherit default values from a template
host_name allan.com ; The name we are giving to this host
alias allan ; A longer name associated with the host
address 123.123.123.123 ; IP address of the host
}
解释:
./sed.sh
host_name :
allan.com
alias :
allan
address :
123.123.123.123
cat windows.cfg
#some settings
#more settings
test
abc
lot of stuff
test2
backup
address of the host }
define host{
use windows-server ; Inherit default values from a template
host_name allan.com ; The name we are giving to this host
alias allan ; A longer name associated with the host
address 123.123.123.123 ; IP address of the host
}
我决定在
sed
中使用r
命令(读取文件内容以插入文件),而不是使用a
。然后我使用符号变量总是在双引号内被替换,我看不出为什么它在这里不起作用。如果用文字字符串替换变量,是否有效?主机地址}a
应该是/host地址/a
我尝试使用/option/但这否定了变量。现在正在调用变量…但我在运行脚本时收到了这个消息:expression#1,char 28:commands后面的额外字符如果您想查看shell脚本正在做什么,请将set-x放到开头。然后它将显示所有正在执行的命令,并填入变量。@Allan是的,您的解释写得非常好,有助于澄清分配。我还按照建议执行了-x命令,以查看该命令实际上也在做什么,这很有用。脚本运行时没有错误…但windows.cfg文件没有更改。