Test if yad version >= specific version numberCheck if Bash version is >= given version numberWhich version control system to use?How to fake Ubuntu version number to applicationsYad list columnsIs there some index of Ubuntu architecture/version number combinations?How to edit YAD question labels (example)?How do I get the version number of a snap package?Check if Bash version is >= given version numberWhy is bash denying my shell script permission to open a text file with default text editor?
Counterpart of cyclotomic polynomials for elliptic divisibility sequences
How much energy does a bee/micro robot need per second of flight?
What would cause vast differences in lifespans between the sexes?
Ubuntu: FireFox: Strange certificates
Is it possible to get reverse life insurance?
Should I report a security vulnerability?
Space complexity for storing integers in Python
Are humans superior to machines in chess?
How do soldiers of conquered states enlist into the army of their conqueror?
Paying to leave without notice in at-will employment state
Sun dried tomatoes
Why use Fourier series instead of Taylor?
What was meant by the protest sign "Bundestag nach Berlin"?
How to manage publications on a local computer
Income too high for a Roth IRA
Is it possible to use gases instead of liquids as fuel in a rocket engine?
A randomized encryption program
Where is a warlock's soul?
Bo Derek in texbook.tex?
How did Krennic locate the Erso family's hideout?
Getting data from Seagate ST-238R drive
Cases bugged - lines overlapping
About an ambiguity that really prevents me from understanding the principle " the laws of physics are invariant in all inertial frames"
Font size in pmatrix: Elegant Summation in big Vectors
Test if yad version >= specific version number
Check if Bash version is >= given version numberWhich version control system to use?How to fake Ubuntu version number to applicationsYad list columnsIs there some index of Ubuntu architecture/version number combinations?How to edit YAD question labels (example)?How do I get the version number of a snap package?Check if Bash version is >= given version numberWhy is bash denying my shell script permission to open a text file with default text editor?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I need a script to check if yad (and other programs) version number is >= to a specific number. For example I have:
$ yad --version
0.40.0 (GTK+ 3.24.8)
$ gedit --version
gedit - Version 3.32.0
$ bash --version
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
- For yad new features are added between Ubuntu 16.04 and 19.04
- For gedit the ability to pass Window geometry is lost in newer versions
bash complicates tests as the version number is in the middle of the first line.
An environment variable will not exist for all programs like bash has:
$ echo $BASH_VERSION
5.0.3(1)-release
bash versions version-control yad
add a comment
|
I need a script to check if yad (and other programs) version number is >= to a specific number. For example I have:
$ yad --version
0.40.0 (GTK+ 3.24.8)
$ gedit --version
gedit - Version 3.32.0
$ bash --version
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
- For yad new features are added between Ubuntu 16.04 and 19.04
- For gedit the ability to pass Window geometry is lost in newer versions
bash complicates tests as the version number is in the middle of the first line.
An environment variable will not exist for all programs like bash has:
$ echo $BASH_VERSION
5.0.3(1)-release
bash versions version-control yad
add a comment
|
I need a script to check if yad (and other programs) version number is >= to a specific number. For example I have:
$ yad --version
0.40.0 (GTK+ 3.24.8)
$ gedit --version
gedit - Version 3.32.0
$ bash --version
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
- For yad new features are added between Ubuntu 16.04 and 19.04
- For gedit the ability to pass Window geometry is lost in newer versions
bash complicates tests as the version number is in the middle of the first line.
An environment variable will not exist for all programs like bash has:
$ echo $BASH_VERSION
5.0.3(1)-release
bash versions version-control yad
I need a script to check if yad (and other programs) version number is >= to a specific number. For example I have:
$ yad --version
0.40.0 (GTK+ 3.24.8)
$ gedit --version
gedit - Version 3.32.0
$ bash --version
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
- For yad new features are added between Ubuntu 16.04 and 19.04
- For gedit the ability to pass Window geometry is lost in newer versions
bash complicates tests as the version number is in the middle of the first line.
An environment variable will not exist for all programs like bash has:
$ echo $BASH_VERSION
5.0.3(1)-release
bash versions version-control yad
bash versions version-control yad
edited Aug 5 at 18:53
K7AAY
6,6305 gold badges21 silver badges49 bronze badges
6,6305 gold badges21 silver badges49 bronze badges
asked Aug 5 at 16:50
WinEunuuchs2UnixWinEunuuchs2Unix
60.9k18 gold badges123 silver badges237 bronze badges
60.9k18 gold badges123 silver badges237 bronze badges
add a comment
|
add a comment
|
3 Answers
3
active
oldest
votes
You might want to try GNU sort
's -V
(--version-sort
), along with -C
(--check=quiet
):
$ echo $BASH_VERSION
4.4.20(1)-release
then to return 0
(true) if the version is at least the given one and 1
(false) otherwise:
$ printf '%sn%sn' "$BASH_VERSION" "4.3" | sort -rVC ; echo $?
0
$ printf '%sn%sn' "$BASH_VERSION" "4.4.20(2)" | sort -rVC ; echo $?
1
2
Apologies I posted two similar questions about the same time. This answer is perfect for the other question: askubuntu.com/questions/916976/…
– WinEunuuchs2Unix
Aug 5 at 17:23
add a comment
|
I developed a script that draws on answers in Stack Overflow. One of those answers led to a Dell Employee writing version number comparisons in 2004 for the DKMS application.
Sample Tests
$ testver yad 0.40.0; echo $?
0
$ testver yad 0.41.0; echo $?
1
$ testver bash 5.0.3; echo $?
0
$ testver bash 5.0.4; echo $?
1
$ testver gedit 3.32.0; echo $?
0
$ testver gedit 4.32.0; echo $?
1
$ testver iwconfig 30; echo $?
0
$ testver iwconfig 31; echo $?
1
Real life application
if testver gnome-shell 3.32.0 ; then
# returns 0 version 3.32.0 and greater geometry not supported.
nohup gedit $@ &>/dev/null &
else
# returns 1 version less than 3.32.0 so geometry supported.
nohup gedit -g 1300x840+4565+2345 $@ &>/dev/null &
fi
The code
The bash script below needs to be marked as executable using the command chmod a+x script-name
. I'm using the name /usr/local/bin/testver
:
#!/bin/bash
# NAME: testver
# PATH: /usr/local/bin
# DESC: Test a program's version number >= to passed version number
# DATE: May 21, 2017. Modified August 5, 2019.
# CALL: testver Program Version
# PARM: 1. Program - validated to be a command
# 2. Version - validated to be numberic
# NOTE: Extracting version number perl one-liner found here:
# http://stackoverflow.com/questions/16817646/extract-version-number-from-a-string
# Comparing two version numbers code found here:
# http://stackoverflow.com/questions/4023830/how-compare-two-strings-in-dot-separated-version-format-in-bash
# Map parameters to coder-friendly names.
Program="$1"
Version="$2"
# Program name must be a valid command.
command -v $Program >/dev/null 2>&1 || echo "Command: $Program not found. Check spelling."; exit 99;
# Passed version number must be valid format.
if ! [[ $Version =~ ^([0-9]+.?)+$ ]]; then
echo "Version number: $Version has invalid format. Aborting.";
exit 99
fi
InstalledVersion=$( "$Program" --version | perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' )
# Perl command doesn't work for non-decimal version numbers
[[ "$InstalledVersion" == "" ]] &&
InstalledVersion=$( "$Program" --version | head -n1 | tr -dc '0-9')
if [[ $InstalledVersion =~ ^([0-9]+.?)+$ ]]; then
l=($InstalledVersion//./ )
r=($Version//./ )
s=$#l[@]
[[ $#r[@] -gt $#l[@] ]] && s=$#r[@]
for i in $(seq 0 $((s - 1))); do
# echo "Installed $l[$i] -gt Test $r[$i]?"
[[ $l[$i] -gt $r[$i] ]] && exit 0 # Installed version > test version.
[[ $l[$i] -lt $r[$i] ]] && exit 1 # Installed version < test version.
done
exit 0 # Installed version = test version.
else
echo "Invalid version number: $InstalledVersion found for command: $Program"
exit 99
fi
echo "testver - Unreachable code has been reached!"
exit 255
add a comment
|
You can use dpkg --compare-versions
. Usage example:
$ dpkg --compare-versions 4.0 lt 5.0 && echo true
true
This returns "true" because version 4.0 is less than ("lt") version 5.0.
On the other hand the following does not return anything:
$ dpkg --compare-versions 4.0 gt 5.0 && echo true
This is because version 4.0 is not greater ("gt") than 5.0.
Comparison operators for dpkg --compare-versions
are:
lt le eq ne ge gt
(treat empty version as earlier than any
version);lt-nl le-nl ge-nl gt-nl
(treat empty version as later
than any version);< << <= = >= >> >
(only for compatibility with control file syntax).
add a comment
|
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1163600%2ftest-if-yad-version-specific-version-number%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You might want to try GNU sort
's -V
(--version-sort
), along with -C
(--check=quiet
):
$ echo $BASH_VERSION
4.4.20(1)-release
then to return 0
(true) if the version is at least the given one and 1
(false) otherwise:
$ printf '%sn%sn' "$BASH_VERSION" "4.3" | sort -rVC ; echo $?
0
$ printf '%sn%sn' "$BASH_VERSION" "4.4.20(2)" | sort -rVC ; echo $?
1
2
Apologies I posted two similar questions about the same time. This answer is perfect for the other question: askubuntu.com/questions/916976/…
– WinEunuuchs2Unix
Aug 5 at 17:23
add a comment
|
You might want to try GNU sort
's -V
(--version-sort
), along with -C
(--check=quiet
):
$ echo $BASH_VERSION
4.4.20(1)-release
then to return 0
(true) if the version is at least the given one and 1
(false) otherwise:
$ printf '%sn%sn' "$BASH_VERSION" "4.3" | sort -rVC ; echo $?
0
$ printf '%sn%sn' "$BASH_VERSION" "4.4.20(2)" | sort -rVC ; echo $?
1
2
Apologies I posted two similar questions about the same time. This answer is perfect for the other question: askubuntu.com/questions/916976/…
– WinEunuuchs2Unix
Aug 5 at 17:23
add a comment
|
You might want to try GNU sort
's -V
(--version-sort
), along with -C
(--check=quiet
):
$ echo $BASH_VERSION
4.4.20(1)-release
then to return 0
(true) if the version is at least the given one and 1
(false) otherwise:
$ printf '%sn%sn' "$BASH_VERSION" "4.3" | sort -rVC ; echo $?
0
$ printf '%sn%sn' "$BASH_VERSION" "4.4.20(2)" | sort -rVC ; echo $?
1
You might want to try GNU sort
's -V
(--version-sort
), along with -C
(--check=quiet
):
$ echo $BASH_VERSION
4.4.20(1)-release
then to return 0
(true) if the version is at least the given one and 1
(false) otherwise:
$ printf '%sn%sn' "$BASH_VERSION" "4.3" | sort -rVC ; echo $?
0
$ printf '%sn%sn' "$BASH_VERSION" "4.4.20(2)" | sort -rVC ; echo $?
1
answered Aug 5 at 17:20
steeldriversteeldriver
81.6k12 gold badges133 silver badges222 bronze badges
81.6k12 gold badges133 silver badges222 bronze badges
2
Apologies I posted two similar questions about the same time. This answer is perfect for the other question: askubuntu.com/questions/916976/…
– WinEunuuchs2Unix
Aug 5 at 17:23
add a comment
|
2
Apologies I posted two similar questions about the same time. This answer is perfect for the other question: askubuntu.com/questions/916976/…
– WinEunuuchs2Unix
Aug 5 at 17:23
2
2
Apologies I posted two similar questions about the same time. This answer is perfect for the other question: askubuntu.com/questions/916976/…
– WinEunuuchs2Unix
Aug 5 at 17:23
Apologies I posted two similar questions about the same time. This answer is perfect for the other question: askubuntu.com/questions/916976/…
– WinEunuuchs2Unix
Aug 5 at 17:23
add a comment
|
I developed a script that draws on answers in Stack Overflow. One of those answers led to a Dell Employee writing version number comparisons in 2004 for the DKMS application.
Sample Tests
$ testver yad 0.40.0; echo $?
0
$ testver yad 0.41.0; echo $?
1
$ testver bash 5.0.3; echo $?
0
$ testver bash 5.0.4; echo $?
1
$ testver gedit 3.32.0; echo $?
0
$ testver gedit 4.32.0; echo $?
1
$ testver iwconfig 30; echo $?
0
$ testver iwconfig 31; echo $?
1
Real life application
if testver gnome-shell 3.32.0 ; then
# returns 0 version 3.32.0 and greater geometry not supported.
nohup gedit $@ &>/dev/null &
else
# returns 1 version less than 3.32.0 so geometry supported.
nohup gedit -g 1300x840+4565+2345 $@ &>/dev/null &
fi
The code
The bash script below needs to be marked as executable using the command chmod a+x script-name
. I'm using the name /usr/local/bin/testver
:
#!/bin/bash
# NAME: testver
# PATH: /usr/local/bin
# DESC: Test a program's version number >= to passed version number
# DATE: May 21, 2017. Modified August 5, 2019.
# CALL: testver Program Version
# PARM: 1. Program - validated to be a command
# 2. Version - validated to be numberic
# NOTE: Extracting version number perl one-liner found here:
# http://stackoverflow.com/questions/16817646/extract-version-number-from-a-string
# Comparing two version numbers code found here:
# http://stackoverflow.com/questions/4023830/how-compare-two-strings-in-dot-separated-version-format-in-bash
# Map parameters to coder-friendly names.
Program="$1"
Version="$2"
# Program name must be a valid command.
command -v $Program >/dev/null 2>&1 || echo "Command: $Program not found. Check spelling."; exit 99;
# Passed version number must be valid format.
if ! [[ $Version =~ ^([0-9]+.?)+$ ]]; then
echo "Version number: $Version has invalid format. Aborting.";
exit 99
fi
InstalledVersion=$( "$Program" --version | perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' )
# Perl command doesn't work for non-decimal version numbers
[[ "$InstalledVersion" == "" ]] &&
InstalledVersion=$( "$Program" --version | head -n1 | tr -dc '0-9')
if [[ $InstalledVersion =~ ^([0-9]+.?)+$ ]]; then
l=($InstalledVersion//./ )
r=($Version//./ )
s=$#l[@]
[[ $#r[@] -gt $#l[@] ]] && s=$#r[@]
for i in $(seq 0 $((s - 1))); do
# echo "Installed $l[$i] -gt Test $r[$i]?"
[[ $l[$i] -gt $r[$i] ]] && exit 0 # Installed version > test version.
[[ $l[$i] -lt $r[$i] ]] && exit 1 # Installed version < test version.
done
exit 0 # Installed version = test version.
else
echo "Invalid version number: $InstalledVersion found for command: $Program"
exit 99
fi
echo "testver - Unreachable code has been reached!"
exit 255
add a comment
|
I developed a script that draws on answers in Stack Overflow. One of those answers led to a Dell Employee writing version number comparisons in 2004 for the DKMS application.
Sample Tests
$ testver yad 0.40.0; echo $?
0
$ testver yad 0.41.0; echo $?
1
$ testver bash 5.0.3; echo $?
0
$ testver bash 5.0.4; echo $?
1
$ testver gedit 3.32.0; echo $?
0
$ testver gedit 4.32.0; echo $?
1
$ testver iwconfig 30; echo $?
0
$ testver iwconfig 31; echo $?
1
Real life application
if testver gnome-shell 3.32.0 ; then
# returns 0 version 3.32.0 and greater geometry not supported.
nohup gedit $@ &>/dev/null &
else
# returns 1 version less than 3.32.0 so geometry supported.
nohup gedit -g 1300x840+4565+2345 $@ &>/dev/null &
fi
The code
The bash script below needs to be marked as executable using the command chmod a+x script-name
. I'm using the name /usr/local/bin/testver
:
#!/bin/bash
# NAME: testver
# PATH: /usr/local/bin
# DESC: Test a program's version number >= to passed version number
# DATE: May 21, 2017. Modified August 5, 2019.
# CALL: testver Program Version
# PARM: 1. Program - validated to be a command
# 2. Version - validated to be numberic
# NOTE: Extracting version number perl one-liner found here:
# http://stackoverflow.com/questions/16817646/extract-version-number-from-a-string
# Comparing two version numbers code found here:
# http://stackoverflow.com/questions/4023830/how-compare-two-strings-in-dot-separated-version-format-in-bash
# Map parameters to coder-friendly names.
Program="$1"
Version="$2"
# Program name must be a valid command.
command -v $Program >/dev/null 2>&1 || echo "Command: $Program not found. Check spelling."; exit 99;
# Passed version number must be valid format.
if ! [[ $Version =~ ^([0-9]+.?)+$ ]]; then
echo "Version number: $Version has invalid format. Aborting.";
exit 99
fi
InstalledVersion=$( "$Program" --version | perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' )
# Perl command doesn't work for non-decimal version numbers
[[ "$InstalledVersion" == "" ]] &&
InstalledVersion=$( "$Program" --version | head -n1 | tr -dc '0-9')
if [[ $InstalledVersion =~ ^([0-9]+.?)+$ ]]; then
l=($InstalledVersion//./ )
r=($Version//./ )
s=$#l[@]
[[ $#r[@] -gt $#l[@] ]] && s=$#r[@]
for i in $(seq 0 $((s - 1))); do
# echo "Installed $l[$i] -gt Test $r[$i]?"
[[ $l[$i] -gt $r[$i] ]] && exit 0 # Installed version > test version.
[[ $l[$i] -lt $r[$i] ]] && exit 1 # Installed version < test version.
done
exit 0 # Installed version = test version.
else
echo "Invalid version number: $InstalledVersion found for command: $Program"
exit 99
fi
echo "testver - Unreachable code has been reached!"
exit 255
add a comment
|
I developed a script that draws on answers in Stack Overflow. One of those answers led to a Dell Employee writing version number comparisons in 2004 for the DKMS application.
Sample Tests
$ testver yad 0.40.0; echo $?
0
$ testver yad 0.41.0; echo $?
1
$ testver bash 5.0.3; echo $?
0
$ testver bash 5.0.4; echo $?
1
$ testver gedit 3.32.0; echo $?
0
$ testver gedit 4.32.0; echo $?
1
$ testver iwconfig 30; echo $?
0
$ testver iwconfig 31; echo $?
1
Real life application
if testver gnome-shell 3.32.0 ; then
# returns 0 version 3.32.0 and greater geometry not supported.
nohup gedit $@ &>/dev/null &
else
# returns 1 version less than 3.32.0 so geometry supported.
nohup gedit -g 1300x840+4565+2345 $@ &>/dev/null &
fi
The code
The bash script below needs to be marked as executable using the command chmod a+x script-name
. I'm using the name /usr/local/bin/testver
:
#!/bin/bash
# NAME: testver
# PATH: /usr/local/bin
# DESC: Test a program's version number >= to passed version number
# DATE: May 21, 2017. Modified August 5, 2019.
# CALL: testver Program Version
# PARM: 1. Program - validated to be a command
# 2. Version - validated to be numberic
# NOTE: Extracting version number perl one-liner found here:
# http://stackoverflow.com/questions/16817646/extract-version-number-from-a-string
# Comparing two version numbers code found here:
# http://stackoverflow.com/questions/4023830/how-compare-two-strings-in-dot-separated-version-format-in-bash
# Map parameters to coder-friendly names.
Program="$1"
Version="$2"
# Program name must be a valid command.
command -v $Program >/dev/null 2>&1 || echo "Command: $Program not found. Check spelling."; exit 99;
# Passed version number must be valid format.
if ! [[ $Version =~ ^([0-9]+.?)+$ ]]; then
echo "Version number: $Version has invalid format. Aborting.";
exit 99
fi
InstalledVersion=$( "$Program" --version | perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' )
# Perl command doesn't work for non-decimal version numbers
[[ "$InstalledVersion" == "" ]] &&
InstalledVersion=$( "$Program" --version | head -n1 | tr -dc '0-9')
if [[ $InstalledVersion =~ ^([0-9]+.?)+$ ]]; then
l=($InstalledVersion//./ )
r=($Version//./ )
s=$#l[@]
[[ $#r[@] -gt $#l[@] ]] && s=$#r[@]
for i in $(seq 0 $((s - 1))); do
# echo "Installed $l[$i] -gt Test $r[$i]?"
[[ $l[$i] -gt $r[$i] ]] && exit 0 # Installed version > test version.
[[ $l[$i] -lt $r[$i] ]] && exit 1 # Installed version < test version.
done
exit 0 # Installed version = test version.
else
echo "Invalid version number: $InstalledVersion found for command: $Program"
exit 99
fi
echo "testver - Unreachable code has been reached!"
exit 255
I developed a script that draws on answers in Stack Overflow. One of those answers led to a Dell Employee writing version number comparisons in 2004 for the DKMS application.
Sample Tests
$ testver yad 0.40.0; echo $?
0
$ testver yad 0.41.0; echo $?
1
$ testver bash 5.0.3; echo $?
0
$ testver bash 5.0.4; echo $?
1
$ testver gedit 3.32.0; echo $?
0
$ testver gedit 4.32.0; echo $?
1
$ testver iwconfig 30; echo $?
0
$ testver iwconfig 31; echo $?
1
Real life application
if testver gnome-shell 3.32.0 ; then
# returns 0 version 3.32.0 and greater geometry not supported.
nohup gedit $@ &>/dev/null &
else
# returns 1 version less than 3.32.0 so geometry supported.
nohup gedit -g 1300x840+4565+2345 $@ &>/dev/null &
fi
The code
The bash script below needs to be marked as executable using the command chmod a+x script-name
. I'm using the name /usr/local/bin/testver
:
#!/bin/bash
# NAME: testver
# PATH: /usr/local/bin
# DESC: Test a program's version number >= to passed version number
# DATE: May 21, 2017. Modified August 5, 2019.
# CALL: testver Program Version
# PARM: 1. Program - validated to be a command
# 2. Version - validated to be numberic
# NOTE: Extracting version number perl one-liner found here:
# http://stackoverflow.com/questions/16817646/extract-version-number-from-a-string
# Comparing two version numbers code found here:
# http://stackoverflow.com/questions/4023830/how-compare-two-strings-in-dot-separated-version-format-in-bash
# Map parameters to coder-friendly names.
Program="$1"
Version="$2"
# Program name must be a valid command.
command -v $Program >/dev/null 2>&1 || echo "Command: $Program not found. Check spelling."; exit 99;
# Passed version number must be valid format.
if ! [[ $Version =~ ^([0-9]+.?)+$ ]]; then
echo "Version number: $Version has invalid format. Aborting.";
exit 99
fi
InstalledVersion=$( "$Program" --version | perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' )
# Perl command doesn't work for non-decimal version numbers
[[ "$InstalledVersion" == "" ]] &&
InstalledVersion=$( "$Program" --version | head -n1 | tr -dc '0-9')
if [[ $InstalledVersion =~ ^([0-9]+.?)+$ ]]; then
l=($InstalledVersion//./ )
r=($Version//./ )
s=$#l[@]
[[ $#r[@] -gt $#l[@] ]] && s=$#r[@]
for i in $(seq 0 $((s - 1))); do
# echo "Installed $l[$i] -gt Test $r[$i]?"
[[ $l[$i] -gt $r[$i] ]] && exit 0 # Installed version > test version.
[[ $l[$i] -lt $r[$i] ]] && exit 1 # Installed version < test version.
done
exit 0 # Installed version = test version.
else
echo "Invalid version number: $InstalledVersion found for command: $Program"
exit 99
fi
echo "testver - Unreachable code has been reached!"
exit 255
edited Aug 5 at 23:33
answered Aug 5 at 16:50
WinEunuuchs2UnixWinEunuuchs2Unix
60.9k18 gold badges123 silver badges237 bronze badges
60.9k18 gold badges123 silver badges237 bronze badges
add a comment
|
add a comment
|
You can use dpkg --compare-versions
. Usage example:
$ dpkg --compare-versions 4.0 lt 5.0 && echo true
true
This returns "true" because version 4.0 is less than ("lt") version 5.0.
On the other hand the following does not return anything:
$ dpkg --compare-versions 4.0 gt 5.0 && echo true
This is because version 4.0 is not greater ("gt") than 5.0.
Comparison operators for dpkg --compare-versions
are:
lt le eq ne ge gt
(treat empty version as earlier than any
version);lt-nl le-nl ge-nl gt-nl
(treat empty version as later
than any version);< << <= = >= >> >
(only for compatibility with control file syntax).
add a comment
|
You can use dpkg --compare-versions
. Usage example:
$ dpkg --compare-versions 4.0 lt 5.0 && echo true
true
This returns "true" because version 4.0 is less than ("lt") version 5.0.
On the other hand the following does not return anything:
$ dpkg --compare-versions 4.0 gt 5.0 && echo true
This is because version 4.0 is not greater ("gt") than 5.0.
Comparison operators for dpkg --compare-versions
are:
lt le eq ne ge gt
(treat empty version as earlier than any
version);lt-nl le-nl ge-nl gt-nl
(treat empty version as later
than any version);< << <= = >= >> >
(only for compatibility with control file syntax).
add a comment
|
You can use dpkg --compare-versions
. Usage example:
$ dpkg --compare-versions 4.0 lt 5.0 && echo true
true
This returns "true" because version 4.0 is less than ("lt") version 5.0.
On the other hand the following does not return anything:
$ dpkg --compare-versions 4.0 gt 5.0 && echo true
This is because version 4.0 is not greater ("gt") than 5.0.
Comparison operators for dpkg --compare-versions
are:
lt le eq ne ge gt
(treat empty version as earlier than any
version);lt-nl le-nl ge-nl gt-nl
(treat empty version as later
than any version);< << <= = >= >> >
(only for compatibility with control file syntax).
You can use dpkg --compare-versions
. Usage example:
$ dpkg --compare-versions 4.0 lt 5.0 && echo true
true
This returns "true" because version 4.0 is less than ("lt") version 5.0.
On the other hand the following does not return anything:
$ dpkg --compare-versions 4.0 gt 5.0 && echo true
This is because version 4.0 is not greater ("gt") than 5.0.
Comparison operators for dpkg --compare-versions
are:
lt le eq ne ge gt
(treat empty version as earlier than any
version);lt-nl le-nl ge-nl gt-nl
(treat empty version as later
than any version);< << <= = >= >> >
(only for compatibility with control file syntax).
answered Aug 6 at 11:59
LogixLogix
1,0755 silver badges16 bronze badges
1,0755 silver badges16 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1163600%2ftest-if-yad-version-specific-version-number%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown