I was having a xml file that contains user-name and password for multiple times, and also connection-url which needs to be changed dynamically.
<datasources>
<datasource jndi-name="java:jboss/datasources/TestFlow" pool-name="TestFlow" enabled="true" use-java-context="true" statistics-enabled="${wildfly.datasources.statistics-enabled:$ {wildfly.statistics-enabled:false}}">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
<driver>h2</driver>
<security>
<user-name>test</user-name>
<password>test</password>
</security>
</datasource>
<datasource jta="false" jndi-name="java:/AdminDSource" poolname="AdminDSource" enabled="true" use-java-context="true">
<connection-url>jdbc:oracle:thin:@xxxxxx.xxxxxxx.xxxxxxxx-1.rds.amazonaws.com:xxxx:ORCL</connection-url>
<driver>oracle</driver>
<security>
<user-name>aldo</user-name>
<password>aldo</password>
</security>
</datasource>
</datasources>
In the above I would like to change the first occurrence of connection-url, user-name and password to be replaced with some desired values
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
<user-name>test</user-name>
<password>test</password>
to be changed to
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-2;DB_CLOSE_ON_EXIT=FALSE</connection-url>
<user-name>Atom</user-name>
<password>Atom</password>
Similarly for the second occurrence of the same to be changed
<connection-url>jdbc:oracle:thin:@{Content after the @ to be changed}</connection-url>
<user-name>{aldo to username}</user-name>
<password>{aldo to password}</password>
I have tried the below to update user-name and password,
for filename in *.xml; do
if grep -q '<driver>h2</driver>' "$filename"; then
sed -i.bak 's/<user-name>test<\/user-name>/<user-name>Atom<\/user-name>/g' "$filename"
fi
if grep -q '<driver>h2</driver>' "$filename"; then
sed -i.bak 's/<password>test<\/password>/<password>Atom<\/password>/g' "$filename"
fi
if grep -q '<driver>oracle</driver>' "$filename"; then
sed -i.bak 's/<user-name>aldo<\/user-name>/<user-name>username<\/user-name>/g' "$filename"
fi
if grep -q '<driver>oracle</driver>' "$filename"; then
sed -i.bak 's/<password>aldo<\/password>/<password>password<\/password>/g' "$filename"
fi
done
but I would like to have a single script that makes all the desirable changes.