使用 xmlstarlet 和 BASH,如何仅在元素具有文本字符串"${numberOFThreads}"时才更新元素的值。仅当字符串 ${numberOFThreads} 出现数字 20 时,我才尝试替换该项目。
以前:
<stringProp name="ThreadGroup.num_threads">"${numberOFThreads}"</stringProp>
后:
<stringProp name="ThreadGroup.num_threads">20</stringProp>
测试XML
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.8" jmeter="2.13 r1665067">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
<stringProp name="TestPlan.comments"></stringProp>
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">1</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">${numberOFThreads}"</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<longProp name="ThreadGroup.start_time">1456663030000</longProp>
<longProp name="ThreadGroup.end_time">1456663030000</longProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
</ThreadGroup>
<hashTree/>
</hashTree>
</hashTree>
</jmeterTestPlan>
您可以使用 [. = ".."]
过滤元素的值。你只需要小心,在转义"$
字符时:
xmlstarlet ed -u "//stringProp[@name='ThreadGroup.num_threads' and . = '"${numberOFThreads}"']" -v "20" /tmp/test.xml