这是做到这一点的一种方法,它尝试使用shell函数以bash(版本> 4.2)为主要实现对象。
这不会返回最终状态代码,只是将报告输出到stdout。
#!/usr/bin/env bash
# Compare V1 to V2 return 2 if V2 is higher than V1
function compare_version_strings()
{
local v1=( $(echo "$1" | tr '.' ' ') )
local v2=( $(echo "$2" | tr '.' ' ') )
local len=${#v1[*]}
if [ "${#v2[*]}" -gt "$len" ] ;
then
len="${#v2[*]}" ;
fi
for ((i=0; i<len; i++))
do
[ "${v1[i]:-0}" -gt "${v2[i]:-0}" ] && return 1 # v1 higher
[ "${v1[i]:-0}" -lt "${v2[i]:-0}" ] && return 2 # v2 higher
done
return 0 # same version
}
# Wrapper around compare_version_strings
function check_tool_version()
{
local tool="$1" && shift
local vmin="$1" && shift
local vact="$1" && shift
compare_version_strings "$vact" "$vmin"
local result="$?"
echo -n checking for $tool $vmin
if [ "$result" -eq 2 ]; then
echo " [FAIL] -- found $vact"
else
echo " [PASS] -- found $vact"
fi
}
check_tool_version cmake 3.11.2 $(cmake --version | head -1 | awk '{print $3}')
check_tool_version gcc 4.8 $(gcc --version | head -1 | awk '{print $3}')
check_tool_version make 9.1 $(make --version | head -1 | awk '{print $3}')
...以及示例输出...
checking for cmake 3.11.2 [PASS] -- found 3.12.1
checking for gcc 4.8 [PASS] -- found 4.8.5
checking for make 9.1 [FAIL] -- found 3.82
本文链接:https://www.f2er.com/3143345.html