Home > Mobile >  Shell Script - Check if XML tags match version
Shell Script - Check if XML tags match version

Time:11-25

I am quite new to shell scripting and need to make a script that checks xml tag within diferent xml files and diferent xml hierarchy and checks if version matches in different files.

But one extra step is I only need the version number within this tags, some tags may have more information (see examples - snapshot, release).

I am quite new to this, I have used this sed command to get the version, but can´t go much further than that....

Sucess and Fail Examples:

Hello, I am quite new to shell scripting and need to make a script that checks xml tag within diferent xml files and diferent xml hierarchy and checks if version matches in different files.

But one extra step is I only need the version number within this tags, some tags may have more information (see examples - snapshot, release).

I am quite new to this, I have used this sed command to get the version, but can´t go much further than that....

Sucess and Fail Examples:

Example 1 - Sucess

File1.xml
<project>
<version>1.2.4-SNAPSHOT</version>
</project>

File2.xml
<project>
<parent>
<version>1.2.4</version>
</parent>
</project>

File3.xml
<project>
<parent>
<child>
<version>1.2.4-RELEASE</version>
</parent>
</child>
</parent>
</project>

OUTPUT - Versions Match - 1.0.4

Example 2 - Error

File1.xml
<project>
<version>1.2.4-SNAPSHOT</version>
</project>

File2.xml
<project>
<parent>
<version>2.2.2</version>
</parent>
</project>

File3.xml
<project>
<parent>
<child>
<version>1.2.2-RELEASE</version>
</parent>
</child>
</parent>
</project>

OUTPUT - Version Mismatch - Check Version

CodePudding user response:

Since you are dealing with xml files, you should use a proper xml parser to read the file and xpath to search it.

As an aside, File3.xml (in both cases) is not well formed. So, for example, assuming this particular file content is:

<project>
  <parent>
     <child>
        <version>1.2.2-RELEASE</version>
     </child>
  </parent>
</project>

You can use, for example, either xmlstarlet:

xmlstarlet sel -T -t -m "//project//version" -v . File3.xml

or xidel (which I personally prefer because of its support for xpath>1.0):

xidel File3.xml -e '//project//version'

In either case, the output should be

1.2.2-RELEASE

CodePudding user response:

#!/bin/bash

VERSIONS=( $(cat file*.xml | grep -E "^<version>.*</version>$" | cut -d '>' -f 2 | cut -d '<' -f1 | cut -d '-' -f1) )
UNIQUE_VERSION=($(echo "${VERSIONS[@]}" | tr ' ' '\n' | sort -u))
if [ "${VERSIONS[0]}" == "${VERSIONS[1]}" ] && [ "${VERSIONS[1]}" == "${VERSIONS[2]}" ];
then
    echo "Versions are the same. $UNIQUE_VERSION"
    exit 0
else 
    echo "ERROR: Versions do not match. $UNIQUE_VERSION"
    exit 1
fi

cat the files (assuming there is a naming pattern and files are in the same directly)
Filter the version using grep and cut
Get the unique value only using sort -u
The output of the command is saved into an array VERSIONS=( $(command) )

Compare the values within the array and output message if they match or not including the unique version number.

Output when success:
Versions are the same. 1.2.4
Output when failure:
ERROR: Versions do not match. 2.2.2

  • Related