2016-10-20 12 views
-2

bashシェルスクリプトからXMLファイルを編集したいですか? 私は解決策を見つけ出すことに固執しています。これがbashシェルスクリプトで解決できる場合は、あなたの提案を分かち合いましょう。bashシェルからXMLファイルを編集する

入力ファイル(サーバーに保存されます):それはDcustom.properties =/FS0 /を持っている場合、私はapplicationServerInstanceタグを検索して見てみたかった上記のスニペットで

<?xml version="1.0" encoding="UTF-8"?> 
 
<properties> 
 
\t <directories installDir="/fs0/"> 
 
\t \t <directoriesInstance id="sharedDir" path=""/> 
 
\t \t <directoriesInstance id="loaderInput" path="/fs0/share/iad/input"/> 
 
\t \t <directoriesInstance id="loaderProcessing" path="/fs0/share/iad/processing"/> 
 
\t \t <directoriesInstance id="loaderError" path="/fs0/share/iad/error"/> 
 
\t \t <directoriesInstance id="loaderCompleted" path="/fs0/share/iad/completed"/> 
 
\t </directories> 
 
    
 
    <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/share/custom.properties" maxThreads="1000" programParameters="" distributed="false" runJobScheduler="false" useSSO="true" maxConcurrentJobs="10" runProcessEngine="false" messageTimeToLive="120" messageReceiverInterval="5" exceptionRunInterval="normal" maxXmlNodesLimit="150000"/> 
 
    <applicationServer> 
 
\t \t <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" maxThreads="1000" programParameters="" distributed="false" runJobScheduler="false" useSSO="true" maxConcurrentJobs="10" runProcessEngine="false" messageTimeToLive="120" messageReceiverInterval="5" exceptionRunInterval="normal" maxXmlNodesLimit="150000"/> 
 
\t </applicationServer> 
 

 
</properties>

jvmpropertiesタグのclarity1/share/custom.properties - それが存在しない場合は、ファイルに追加してファイルを保存したかったのです。 すべてはbashシェルスクリプトでなければなりません。

私はそれが存在しない場合はapplicationServerInstanceタグでjvmParameters属性に-Dcustom.properties =/FS0 /共有/ custom.propertiesに値を追加します。上記の例ではこの値を持っていますが、存在しない場合は-Dcustom.properties =/fs0/share/custom.propertiesの値を追加します。

例と言う私はjvmParameters以下のスニペットにapplicationServerInstanceタグの属性に-Dcustom.properties =/FS0 /共有/ custom.propertiesに値を追加しなければなりません。あなたのXMLファイルで

<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 " maxThreads="1000" programParameters="" distributed="false" runJobScheduler="false" useSSO="true" maxConcurrentJobs="10" runProcessEngine="false" messageTimeToLive="120" messageReceiverInterval="5" exceptionRunInterval="normal" maxXmlNodesLimit="150000"/>

+0

'jvmproperties' *タグはありませんが、値は' jvmParameters' *属性*に含まれています。値を追加するか、既存の 'custom.properties'を置き換えますか? – choroba

+0

** --Dcustom.properties =/fs0/share/custom.properties **の値を** jvmParameters **属性に** appServerServerInstance **タグに追加します。上記の例ではこの値を持っていますが、存在しなければこの** - Dcustom.properties =/fs0/share/custom.properties **の値を追加したいと思います。 – Naga

+0

コマンドラインXML処理については、xmlstarletを参照してください。 –

答えて

0

、最初applicationServerInstanceタグが-Dcustom.properties =/FS0 /共有/ custom.properties値とjvmParametersを持っています。しかし、2番目のapplicationServerInstanceタグでは、jvmParameterは、-Dcustom.properties =/fs0/share/custom.propertiesではなく、-Dcustom.properties =/fs0/clarity1/share/custom.propertiesの値を持ちます。この場合、私のコードスニペットはDcustom.properties値をもう1つ追加します。

だから、2番目の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"/> 

バッシュスクリプト:

#!/usr/bin/env bash 

file_name="/home/selvam/Scripts/test.xml" 
for i in $(grep -nE "<applicationServerInstance.*jvmParameters" $file_name | cut -d ':' -f1) 
do 
    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 
    fi 
done 
0

AWK

awk -F"jvmParameters=" '{if((/applicationServerInstance/)&&($2!~/Dcustom.properties/)){sub("PrintHeapAtGC","PrintHeapAtGC -Dcustom.properties=/fs0/share/custom.properties")}print}' $inputfile 

は、別の解決策になります。 $ inputfileを、処理したいファイルの名前に置き換えます。

関連する問題