我正在尝试检查我的 cmake 版本是否大于等于 3.13.4,否则如果小于 3.13.4,则构建更新的版本。这是脚本。
#!/bin/sh
RESULT=$(cmake --version)
if [ "$RESULT" -ge 3.13.4 ]; then
echo "you are using the latest version of cmake"
elif [ "$RESULT" -lt 3.13.4 ]; then
cd /home/builduser
mkdir cmake
cd cmake
apt install wget libssl-dev
wget https://github.com/Kitware/CMake/releases/download/v3.22.2/cmake-3.22.2.tar.gz
tar -xf cmake-3.22.2.tar.gz
cd cmake-3.22.2
./configure
make install
cd /home/builduser
rm -rf cmake
fi
答案1
GNU 排序有一个版本排序选项-V
:
cmp=3.13.4
ver=$(cmake --version | head -1 | cut -f3 -d" ")
mapfile -t sorted < <(printf "%s\n" "$ver" "$cmp" | sort -V)
if [[ ${sorted[0]} == "$cmp" ]]; then
echo "cmake version $ver >= $cmp"
fi
答案2
执行此操作的经典方法是使用case
:
case $(cmake --version | awk '/cmake version/ {print $3}') in
[012].*|3.?|3.?.*|3.1[012]|3.1[012].*|3.13|3.13.[0123])
echo Too old
# Download and build
;;
*)
echo OK;;
esac
答案3
-lt
and运算符-gt
只能用于数值比较。 “3.13.4”不是一个数字;它是一个版本字符串。假设我们使用bash
,我们可以编写version_comp
这样的函数:
version_comp() {
local min_version
local have_version
[[ "$1" == "$2" ]] && return 0
mapfile -t -d. min_version <<<"$1"
mapfile -t -d. have_version <<<"$2"
for ((i=0; i<${#min_version[*]}; i++)); do
if (( have_version[i] > min_version[i] )); then
return 0
elif (( have_version[i] < min_version[i] )); then
return 1
fi
done
return 0
}
我们使用mapfile
将版本字符串拆分为数组,这使我们能够以数字方式测试每个组件。
现在我们来测试一下:
target=3.13.3
# Test versions greater than or equal to target
while read -r value; do
if version_comp "$target" "$value"; then
echo "PASSED: $value >= $target"
fi
done << EOF
3.14.4
3.15.0
4
4.0.0
4.1.2
10
EOF
echo
# Test versions less than target
while read -r value; do
if ! version_comp "$target" "$value"; then
echo "PASSED: $value < $target"
fi
done << EOF
2
1.2.3
3.14.2
0
EOF
其产生:
PASSED: 3.14.4 >= 3.13.3
PASSED: 3.15.0 >= 3.13.3
PASSED: 4 >= 3.13.3
PASSED: 4.0.0 >= 3.13.3
PASSED: 4.1.2 >= 3.13.3
PASSED: 10 >= 3.13.3
PASSED: 2 < 3.13.3
PASSED: 1.2.3 < 3.13.3
PASSED: 0 < 3.13.3
答案4
它并不漂亮,但我认为你必须将版本号分成几部分并比较每一部分;你不能使用常规算术,因为例如 3.9 < 3.14。
可能有比这更漂亮的东西,但这就是我想出的:
#!/usr/bin/env bash
# get version and strip any extra junk
result="$(cmake --version | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+')"
# major version number is what precedes first period
major="${result%%.*}"
# take what's left
remainder="${result#*.}"
# what precedes first period in the remainder is the minor version num.
minor="${remainder%.*}"
# revision is what's left after that
revision="${remainder#*.}"
# either the major must be higher,
# or it must be the same and the minor must be higher
# or both must be the same and the release must be high enough
if [ "$major" -gt 3 ] ||
( [ "$major" -eq 3 ] &&
( [ "$minor" -gt 14 ] ||
( [ "$minor" -eq 14 ] && [ "$release" -ge 4 ] ))) ; then
echo "you are using a version of cmake >= 3.14.4"
# version is >= 3.14.4; do stuff
else
echo "you are using an older version of cmake"
# do older version stuff
fi
另一种方法是编写一个递归函数来进行比较:更长一点,但更漂亮、更灵活。
#!/usr/bin/env bash
version_equal_or_greater() {
# read arguments
a="$1"
b="$2"
# if they're identical return true (return code 0)
if [ "$a" == "$b" ] ; then
return 0
fi
# read numbers before first .
amajor="${a%%.*}"
bmajor="${b%%.*}"
# if first is greater return true (return code 0)
if [ "$amajor" -gt "$bmajor" ] ; then
return 0
fi
# if first is less return false (return code 1)
if [ "$amajor" -lt "$bmajor" ] ; then
return 1
fi
# should be the same; compare remainders
version_equal_or_greater "${a#*.}" "${b#*.}"
}
result="$(cmake --version | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+')"
if version_equal_or_greater "$result" "3.14.4" ; then
echo "you are using a version of cmake >= 3.14.4"
# version is >= 3.14.4; do stuff
else
echo "you are using an older version of cmake"
fi