Q. I have a XML file which contain below content, I want to get all IP’s in to one array, Passwords in to other array, Usernames in to third array and instanceCount to other array. Can you show me how write a shell script for this?

My XML file content is

<Users>
<Host>
<hostAddress>180.144.226.47</hostAddress>
<userName>pwdfe</userName>
<password>hjitre</password>
<instanceCount>2</instanceCount>
</Host>
<Host>
<hostAddress>180.144.226.87</hostAddress>
<userName>trrrer</userName>
<password>jhjjhhj</password>
<instanceCount>3</instanceCount>
</Host>
<Host>
<hostAddress>180.455.226.87</hostAddress>
<userName>wewqw</userName>
<password>dfsdfd</password>
<instanceCount>3</instanceCount>
</Host>
</Users>


And the file name is temp.xml   Here is the script which will read these values in to different arrays.   

#!/bin/bash j=1
for i in hostAddress userName password instanceCount
do
arrx=($(grep $i temp.xml | sed -r “s/<(|/)$i>//g”))
eval arr${j}='($(echo ${arrx[@]}))’
((j++))
done
echo “Value of arr1 is ${arr1[@]}”
echo “Value of arr2 is ${arr2[@]}”
echo “Value of arr3 is ${arr3[@]}”
echo “Value of arr4 is ${arr4[@]}”

I used arrays to store the filtered values. Hope this script helps some one.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.