The shell script adds or modifies the xml configuration file of hadoop

When the shell automates the deployment of components, the demand for file strings to process configuration files is relatively large, and the amount of xml in the following format is very large when deploying hadoop:
<property>
<name>yarn.resourcemanager.ha.rm-ids</name>
<value>rm1,rm2</value>
</property>


The following is the value of adding or modifying the yarn.resourcemanager.ha.rm-ids node
#!/bin/bash
filename="/hadoop-2.7.1/etc/hadoop/yarn-site.xml"
key="yarn.resourcemanager.ha.rm-ids"
value="rm1,rm2"
key=${key//\//\\/}
key=${key//\./\\.}
key=${key//\:/\\:}
value=${value//\:/\\:}
value=${value//\//\\/}
value=${value//\./\\.}
regex="<name>\s*${key}\s*<\/name>"
#echo "$regex"
grep -q "$regex" $filename&&isExist="yes"||isExist="no"
if [ $isExist = "yes" ];then
regex2="(${regex}\n*\s*<value>).*?(<\/value>)"
sed -i -r '/'"${regex}"'/{N;s/'"${regex2}"'/\1'"${value}"'\2/g}' $filename
#sed -i -r '/'"${regex}"'/{N;s/'"${regex2}"'/'"${value}"'/g}' $filename
be
if [ $isExist = "no" ];then
sed -i 's/^<\/configuration>/<property>\n<name>'"${key}"'<\/name>\n<value>'"${value}"'<\/value>\n<\/property>\n&/' $filename
be

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326625228&siteId=291194637