Giovanni Tommasini

Be pitiful, for every man is fighting a hard battle.
Ogni persona che incontri sta combattendo una battaglia di cui non sai nulla. Sii gentile. Sempre.
www.giovannitommasini.info

How to put the monit version in a bash variable

10 Feb 2017 » linux, IT, regex, bash, monit

Screenshot monit Screenshot regex

I was looking for a way to save in a variable, the monit version installed in my server

If I use the command monit -V the result is very verbose, like this:

This is Monit version 5.6
Copyright (C) 2001-2013 Tildeslash Ltd. All Rights Reserved.

Instead I would like to only 5.6

So I write a small bash script with a Regular Expression to find and asve in a variable only that 5.6.

#!/bin/bash

m_v="$(monit -V)"

for f in $m_v ; do
    if [[ $f =~ ^[+-]?[0-9]+\.?[0-9]*+\.?[0-9]*$ ]]; then
        monit_version=$f
    fi
done

echo $monit_version

Linkography

  1. https://mmonit.com/monit/
  2. http://www.regular-expressions.info