在你的XML文件中,第一个applicationServerInstance标签有jvmParameters的-Dcustom.properties=/fs0/share/custom.properties值。但在第二个applicationServerInstance标签中,jvmParameter有-Dcustom.properties=/fs0/clarity1/share/custom.properties值,而不是-Dcustom.properties=/fs0/share/custom.properties。在这种情况下,我的代码片段增加了一个Dcustom.properties值。
所以第二个applicationServerInstance标签将是这样的。
<applicationServerInstance id="app" serviceName=" App Server" rmiPort="15001" jvmParameters="-Xmx3072m -Xms512m -XX:-UseGCOverheadLimit -XX:MaxPermSize=196m -Dsun.lang.ClassLoader.allowArraySyntax=true -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/fs0/clarity1/clarity/logs -Xloggc:/fs0/clarity1/clarity/logs/app_gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Dcustom.properties=/fs0/clarity1/share/custom.properties -Dcustom.properties=/fs0/share/custom.properties" maxThreads="1000" programParameters="" distributed="false" runJobScheduler="false" useSSO="true" maxConcurrentJobs="10" runProcessEngine="false" messageTimeToLive="120" messageReceiverInterval="5" exceptionRunInterval="normal" maxXmlNodesLimit="150000"/>
Bash脚本。
#!/usr/bin/env bash
file_name="/home/selvam/Scripts/test.xml"
for i in $(grep -nE "<applicationServerInstance.*jvmParameters" $file_name | cut -d ':' -f1)
jvm_parameters=$(head -$i $file_name | tail -1 | awk -F '"' '{ for (c=1; c<=NF; c++) if ($c ~ /jvmParameters/) print $(c+1)}')
echo $jvm_parameters | grep -q 'Dcustom.properties=/fs0/share/custom.properties'
if ([ $? -ne 0 ]) then
jvm_new_parameters=$(echo $jvm_parameters -Dcustom.properties=/fs0/share/custom.properties)
sed -i "${i}s%jvmParameters=\"$jvm_parameters\"%jvmParameters=\"$jvm_new_parameters\"%" $file_name