8

Which the best way to check that we have minimum require version software in bash script. E.g. git 2.16.2

KOl
  • 105
  • 1
  • 9
  • Hint: What does `git -v` say? – tadman Feb 26 '18 at 22:52
  • @tadman: It says `Unknown option: -v`. There's no general way to get the version of an arbitrary program. Different programs have different options to report their versions (e.g., `git --version`) and use different output formats. You have to do it on a case by case basis. – Keith Thompson Feb 26 '18 at 22:58
  • Okey I can compare with some string which is "git 2.16.2" but how can I check that on Linux is higher version? – KOl Feb 26 '18 at 23:07
  • Check this post: [How to compare two strings in dot separated version format in Bash?](https://stackoverflow.com/a/46437231/6862601) – codeforester Feb 26 '18 at 23:18
  • @KeithThompson Ah, sorry, `git --version`. – tadman Feb 27 '18 at 17:34

4 Answers4

12

git provides its version info like this:

$ git --version
git version 2.11.0

GNU sort understands version numbers and can sort them:

$ (echo min version 2.16.3; git --version) | sort -Vk3
git version 2.11.0
min version 2.16.3
$ (echo min version 2.9.3; git --version) | sort -Vk3
min version 2.9.3
git version 2.11.0

We can combine this to make a test:

if (echo a version 2.16.3; git --version) | sort -Vk3 | tail -1 | grep -q git
then
    echo "Good enough."
else
    echo "Not good"
fi
Daniel
  • 8,212
  • 2
  • 43
  • 36
John1024
  • 109,961
  • 14
  • 137
  • 171
  • You should check your solution, because if current version is equal min version that return "Not good" – KOl Feb 26 '18 at 23:10
  • @KOl Good observation! I just changed the test so that, when version numbers are equal, the fallback alphabetic sort gives the answer we want. – John1024 Feb 26 '18 at 23:26
  • 1
    The command should probably be changed from `echo a` to `echo min` to match the output in the sort example. – Cedric Meury Jul 29 '22 at 07:23
0

There is no general way to detect particular versions of commands.

Different commands use different options. A --version option is common but far from universal. -v and -V are also used by some commands. openssl has openssl version (no hyphen).

And the format in which the version number is displayed varies tremendously. git --version prints:

git version 2.14.1

while perl --version prints:

This is perl 5, version 26, subversion 0 (v5.26.0) built for x86_64-linux-gnu-thread-multi
(with 56 registered patches, see perl -V for more detail)

Copyright 1987-2017, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

with empty lines at the top and bottom.

You have to determine for each command whose version you care about how to get it to print its version number, and how to extract the version number from the output.

Comparing version numbers is, alas, non-trivial. A simple string comparison can fail; for example 1.10 is a higher version than 1.9, but a simple string comparison will reverse that.

One trick, if the version is expressed as a dotted tuple of integers, is:

echo 2.14.1 | perl -pe 's/\d+/sprintf("%04d", $&)/eg'

which gives you "0002.0014.0001". But that could fail for TeX, whose current version number is 3.14159265.

Answers to this question (cited by codeforester in the comments) have some ideas.

You might also want to consider that, for example, versions 4.5 and 5.2 might have a feature or bug fix you need, but 4.4 and 5.1 do not (because the feature was added in 5.2 and backported to the 4.* branch).

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

2018:

What does git -v say? It says Unknown option: -v

Actually, it won't say "Unknown option" anymore.

With Git 2.37 (Q3 2022), "git -v"(man) and "git -h"(man) are now understood as "git --version"(man) and git --help".

See commit 6b52f48 (31 Mar 2022) by Garrit Franke (garritfra).
(Merged by Junio C Hamano -- gitster -- in commit 4976f24, 20 May 2022)

cli: add -v and -h shorthands

Signed-off-by: Garrit Franke

Change the behavior of "git -v"(man) to be synonymous with --version / version, and "git -h"(man) to be synonymous with --help, but not "help".

These shorthands both display the "unknown option" message.
Following this change, "-v" displays the version, and "-h" displays the help text of the "git" command.

It should be noted that the "-v" shorthand could be misinterpreted by the user to mean "verbose" instead of "version", since some sub-commands make use of it in this context.
The top-level "git" command does not have a "verbose" flag, so it's safe to introduce this shorthand unambiguously.

git now includes in its man page:

'git' [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
       ^^               ^^
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Doing something like this in bash can be quite painful. Since Python is available on pretty much all *nix machines nowadays, my general advice is to use bash only for the most basic scripts. Python scripts will be much easier to maintain as they are typically less cryptic.

This particular task can be performed like this:

#!/usr/bin/env python

git version | python -c "import sys;\
_, ver = sys.stdin.read().rsplit(maxsplit=1);\
[int(p) for p in ver.split('.')] < [2, 13, 0]\
    and sys.exit('Your git version is too old!')"
Brecht Machiels
  • 3,181
  • 3
  • 25
  • 38