Linux program version check

How to compare a program’s version in a shell script?

I understood my mistake that I was using strings to compare and the -lt requires integer. So, is there any other way to compare the versions?

There’s also a Stack Overflow question with a bunch of different suggestions for comparing version strings.

9 Answers 9

I don’t know if it is beautiful, but it is working for every version format I know.

#!/bin/bash currentver="$(gcc -dumpversion)" requiredver="5.0.0" if [ "$(printf '%s\n' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then echo "Greater than or equal to $" else echo "Less than $" fi 

(Note: better version by the user ‘wildcard’: https://unix.stackexchange.com/users/135943/wildcard , removed additional condition)

At first I thought this was awful, and then I realized the beauty of shell scripting is precisely in abusing tools like this. +1

This breaks if there are ‘%’ signs in the print statement. Better replace printf «$requiredver\n$currentver» with printf ‘%s\n’ «$requiredver» «$currentver» .

Shorter version, assuming GNU sort :

version_greater_equal() < printf '%s\n%s\n' "$2" "$1" | sort --check=quiet --version-sort >version_greater_equal "$" 8.2 || die "need 8.2 or above" 

Note that it would report gcc_version=008.002 as not being satisfactory even though it sorts the same as 8.2 . That can be alleviated by adding the -s / —stable option to sort .

(1) This is a minor variation of already-given answers. You could add value by adding an explanation, which has not yet been posted. (2) printf ‘%s\n’ is good enough; printf will repeat the format string as needed.

I normally prefer editing existing answers but deleting half of them is tricky: others may see value where I don’t. Same for verbose explanations. Less is more.

I know that printf repeats the format string but I the (lack of!) syntax for this is IMHO obscure; so I use this only when required = when the number of arguments is large or variable.

Here I give a solution for comparing Unix Kernel versions. And it should work for others such as gcc. I only care for the first 2 version number but you can add another layer of logic. It is one liner and I wrote it in multiple line for understanding.

check_linux_version() < version_good=$(uname -r | awk 'BEGIN< FS=".">; < if ($1 < 4) < print "N"; >else if ($1 == 4) < if ($2 < 4) < print "N"; >else < print "Y"; >> else < print "Y"; >>') #if [ "$current" \

You can modify this and use it for gcc version checking.

Читайте также:  Linux размеры разделов при установке

We used to do a lot of version checking in a GNU makefile. We shelled out through the makefile facilities. We had to detect old Binutils and buggy compilers and workaround them on the fly.

#!/usr/bin/env bash CC=$(command -v gcc) GREP=$(command -v grep) # Fixup CC and GREP as needed. It may be needed on AIX, BSDs, and Solaris if [[ -f "/usr/gnu/bin/grep" ]]; then GREP="/usr/gnu/bin/grep" elif [[ -f "/usr/linux/bin/grep" ]]; then GREP="/usr/linux/bin/grep" elif [[ -f "/usr/xpg4/bin/grep" ]]; then GREP="/usr/xpg4/bin/grep" fi # Check compiler for GCC 4.8 or later GCC48_OR_LATER=$("$CXX" -v 2>&1 | "$GREP" -i -c -E "gcc version (4\.9|6\.)") if [[ "$GCC48_OR_LATER" -ne 0 ]]; then . fi # Check assembler for GAS 2.19 or later GAS219_OR_LATER=$("$CXX" -xc -c /dev/null -Wa,-v -o/dev/null 2>&1 | "$GREP" -c -E "GNU assembler version (2\.19|2\.6|7)") if [[ "$GAS219_OR_LATER" -ne 0 ]]; then . fi 

» «$GREP» -i -c -E «gcc version (4\.8|8\.)») » This fails for gcc >=10 (the current version is 11.1.0). I think it might be more future-proof to check if it’s lesser than 4.8 instead. But as a simple fix, the following should probably work for any future major versions (e.g.: 999): «$GREP» -i -c -E «gcc version (4\.8|9\.|98*\.)») . The same issue might affect the GAS check at some point in the future.

function version_compare () < function sub_ver () < local len=$temp=$ && indexOf=`echo $ | echo $` echo -e "$" > function cut_dot () < local offset=$local length=$ echo -e "$<2:((++offset)):length>" > if [ -z "$1" ] || [ -z "$2" ]; then echo "=" && exit 0 fi local v1=`echo -e "$" | tr -d '[[:space:]]'` local v2=`echo -e "$" | tr -d '[[:space:]]'` local v1_sub=`sub_ver $v1` local v2_sub=`sub_ver $v2` if (( v1_sub > v2_sub )); then echo ">" elif (( v1_sub < v2_sub )); then echo "### Usage: version_compare "1.2.3" "1.2.4" # Output:  

With lastversion CLI utility you can compare any arbitrary versions, e.g.:

#> lastversion 1.0.0 -gt 0.9.9 #> 1.0.0 

Exit code 0 when "greater" condition is satisfied.

With zsh , you can use it's n umeric o rdering ( O rdering in reverse) parameter expansion flags:

if at_least $version 1.12; then . fi 

Note that 1.12-pre1 or 1.12-alpha3 would be considered greater than 1.12. 1.012 sorts the same as 1.12 but since one has to come before the other and we do an equality comparison we'll end up considering it not at_least 1.12 .

zsh also comes with an autoload able is-at-least function aimed for that (intended to compare zsh version numbers but can be used with any version provided they use similar versioning scheme as zsh's):

#! /bin/zsh - autoload is-at-least if version=$(gcc -dumpfullversion 2> /dev/null) && [[ -n $version ]] && is-at-least 7.2 $version then . fi 

Note that -dumpfullversion was added in gcc 7.1.0. -dumpversion has been available for decades but note that it may only give the major version number.

From info gcc dumpversion in the manual from gcc-12.2.0:

'-dumpversion'
Print the compiler version (for example, '3.0', '6.3.0' or '7')--and don't do anything else. This is the compiler version used in filesystem paths and specs. Depending on how the compiler has been configured it can be just a single number (major version), two numbers separated by a dot (major and minor version) or three numbers separated by dots (major, minor and patchlevel version).

'-dumpfullversion'
Print the full compiler version--and don't do anything else. The output is always three numbers separated by dots, major, minor and patchlevel version.

$ gcc --version gcc (Debian 12.2.0-14) 12.2.0 Copyright (C) 2022 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ gcc -dumpversion 12 $ gcc -dumpfullversion 12.2.0 

Источник

Как узнать версию программы в Linux

book24 не заставляйте меня думать

sed (GNU sed) 4.7 Packaged by Debian Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Jay Fenlason, Tom Lord, Ken Pizzini, Paolo Bonzini, Jim Meyering, and Assaf Gordon. GNU sed home page: . General help using GNU software: . E-mail bug reports to: .

В Debian , Ubuntu и других производных Debian можно выполнить команду

Например, чтобы узнать версию sed нужно выполнить

Package: sed Essential: yes Priority: required Section: utils Installed-Size: 336 Origin: Ubuntu Maintainer: Ubuntu Developers Bugs: https://bugs.launchpad.net/ubuntu/+filebug Architecture: amd64 Multi-Arch: foreign Version: 4.7-1 Pre-Depends: libacl1 (>= 2.2.51-8), libc6 (>= 2.14), libselinux1 (>= 1.32) Filename: pool/main/s/sed/sed_4.7-1_amd64.deb Size: 189668 MD5sum: 2e2c9a0370c20a2a1921572f250db43e Description: GNU stream editor for filtering/transforming text Original-Maintainer: Clint Adams SHA1: a8106de20fa00fc7f97a45d18ec411512cc64293 SHA256: a5428ddec609149eb6086cec20bf14a0300972a191eb0cc010e7f1c7f17186f4 Homepage: https://www.gnu.org/software/sed/ Task: minimal Description-md5: 2ed71305ee7a49ce4438c58140980d2f

Команда dpkg -p, впрочем как и флаги --version, -V работает не со всеми программами

Например, если вы выполните

Скорее всего получите сообщение

dpkg-query: package 'virtualbox' is not available

Команда virtualbox --version просто запустит Virtualbox

Чтобы определить версию Virtualbox выполните

Oracle VM VirtualBox VM Selector v6.1.10_Ubuntu (C) 2005-2020 Oracle Corporation All rights reserved. No special options. If you are looking for --startvm and related options, you need to use VirtualBoxVM.

  • Поиск по сайту
  • aofeed - Telegram канал чтобы следить за выходом новых статей
  • aofeedchat - задать вопрос в Телеграм-группе

Источник

How to Know the Version of Application Before Installing in Ubuntu

The other day, I was thinking of installing Flowblade, one of the best video editors for Linux. I had two choices for installing this software, either I install it from Ubuntu repositories or from the website of Flowblade itself.

You might already know that the default repository by Ubuntu often doesn’t have the latest versions of a program. Ubuntu does it deliberately to make sure that new version doesn’t have a negative impact on the stability of your system.

But what if you really want only the latest version of an application? You can get it from the official source provided by the provider.

Then comes the question how would you know which version is available to install from Ubuntu?

And this is what I am going to show you in this quick tip. Though I am using Ubuntu here, the same steps are applicable for most other Linux distributions such as Linux Mint, elementary OS etc.

Find out version of a program before installing in Ubuntu

If you read the article about installing software in Ubuntu, you know that you can either use the graphical tool Ubuntu Software Center or the command line itself. We’ll see both ways here.

1. Find out the version of a program before installing in Ubuntu Software Center

Go to Ubuntu Software Center and search for the program you wish to install it. Click on it to find more details about it. You’ll see the information about the version of the program here.

Know the version of a program before installing in Ubuntu Software Center

You’ll also find information about the size of install among other things.

2. Know the version of a program before installing in command line

Like me, if you prefer using the terminal, you can use the command below:

Know the version of a program before installing in terminal

You can also use the old style apt-cache in either of the below two fashions:

Know the version of a program before installing in terminal

Once you find out the software version which you will be getting from the official Ubuntu sources, you can go on to decide if you should be installing it from Ubuntu or from the developer itself.

I hope this quick tip helped you and you learn a new thing about Ubuntu Linux today. Do subscribe to our newsletter to get our articles in your inbox regularly.

Источник

Оцените статью
Adblock
detector