我需要一个脚本来检查亚德 (和其他程序)版本号是否是特定号码的>=。例如,我有:
$ 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.对于bash这样的所有程序,都将存在一个环境变量<#>不存在:
$ echo $BASH_VERSION
5.0.3(1)-release发布于 2019-08-05 17:20:54
您可能想试试GNU sort的S -V (--version-sort),以及-C (--check=quiet):
$ echo $BASH_VERSION
4.4.20(1)-release如果版本至少是给定的版本,则返回0 (true),否则返回1 (false):
$ printf '%s\n%s\n' "$BASH_VERSION" "4.3" | sort -rVC ; echo $?
0
$ printf '%s\n%s\n' "$BASH_VERSION" "4.4.20(2)" | sort -rVC ; echo $?
1发布于 2019-08-05 16:50:35
我开发了一个脚本,它利用堆栈溢出中的答案。其中一个答案导致一名戴尔员工在2004年为DKMS应用程序编写了版本号比较。
$ 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 $?
1if 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下面的bash脚本需要使用命令chmod a+x script-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发布于 2019-08-06 11:59:15
您可以使用dpkg --compare-versions。用法示例:
$ dpkg --compare-versions 4.0 lt 5.0 && echo true
true这将返回"true“,因为4.0版本小于("lt")版本5.0。
另一方面,以下内容不返回任何内容:
$ dpkg --compare-versions 4.0 gt 5.0 && echo true这是因为4.0版本不大于5.0 ("gt")。
dpkg --compare-versions的比较操作符是:
lt le eq ne ge gt (将空版本视为比任何版本都早的版本);lt-nl le-nl ge-nl gt-nl (将空版本视为比任何版本都晚的版本);< << <= = >= >> > (仅为了与控制文件语法兼容)。https://askubuntu.com/questions/1163600
复制相似问题