View Full Version : [SOLVED] Bash. Grab data from a XML file and then check if that data is less than.

April 3rd, 2011, 09:25 AM
Is there any way I can grab some data from a xml file in a bash script and then check if that data (which is a version number) is less than (version number?)

The file is i'm talking about is /usr/share/gtk-engines/murrine.xml

It starts like this:

<?xml version="1.0" encoding="UTF-8"?>
<engine schema_version="0.1" module_name="murrine" long_name="Murrine">

What I want to extract is and then check if the version number (let say 98 ) is lower than 98, and if it is it will echo a message to the user to install the latest murrine engine. Or if 0 is more than 0 then the echo message will be ignored. :) Also, if it can't find /usr/share/gtk-engines/murrine.xml it will display a message to the user to install the murrine engine.

I've been googling forever for something that can check which version of a specific GTK engine someone have installed. Bonus if it works cross-distribution. :P

April 3rd, 2011, 10:51 AM
Make sure to modify the variable at the top for version to check against.
Only uses stuff that comes on almost every distro by default.

Not tested much. Can be improved. But I think it should be sufficient for your case. If you want a more robust solution you'd need to use an XML parser and a real programming language, methinks.

TESTVER variable should only contain one decimal.
If you want to check if it is version or later, set TESTVER to 0.98553.


if [ ! -f "$FILE" ]
echo "$FILE not found. Please install."
exit 1

VERSION=$(grep -m1 '<version>' "$FILE" | sed -e 's/^[ \t]*//' -e 's/<version>\(.*\)<\/version>/\1/g' -e 's/[.]//2g')

echo "You have version: $VERSION"
echo "Version you need: $TESTVER"

if [ `expr $VERSION \>= $TESTVER` -eq 0 ]
echo "You need to upgrade your version"
echo "You are good to go!"

April 3rd, 2011, 11:20 AM
Thank you. Seems to work. :D
Just had to change \>= to \<=. ;)

April 3rd, 2011, 01:38 PM
$ ruby -ne 'puts $_.scan(/<version>(.*?)<\/version>/i)[0][0] if /<version>/' file