Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell(Bash)-验证构建工具并强制执行版本_Bash_Shell - Fatal编程技术网

Shell(Bash)-验证构建工具并强制执行版本

Shell(Bash)-验证构建工具并强制执行版本,bash,shell,Bash,Shell,我想编写一个shell/bash脚本,它可以验证我的所有构建工具是否符合特定的版本要求,并返回pass/failed 例如: 我使用cmake、make和gcc $ cmake --version cmake version 3.13.4 CMake suite maintained and supported by Kitware (kitware.com/cmake). $ make --version GNU Make 4.2.1 Built for x86_64-pc-linux

我想编写一个shell/bash脚本,它可以验证我的所有构建工具是否符合特定的版本要求,并返回pass/failed

例如:

我使用cmake、make和gcc

$ cmake --version

cmake version 3.13.4

CMake suite maintained and supported by Kitware (kitware.com/cmake).

$ make --version

GNU Make 4.2.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

$ gcc --version

gcc (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008
Copyright (C) 2019 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.
$cmake--版本
cmake版本3.13.4
由Kitware(Kitware.com/CMake)维护和支持的CMake套件。
$make--版本
GNU Make 4.2.1
为x86_64-pc-linux-gnu构建
版权所有(C)1988年至2016年自由软件基金会。
许可证GPLv3+:GNU GPL版本3或更高版本
这是自由软件:您可以自由更改和重新发布它。
在法律允许的范围内,不存在任何担保。
$gcc—版本
gcc(Ubuntu 9.2.1-9ubuntu2)9.2.1 20191008
版权所有(C)2019免费软件基金会。
这是自由软件;有关复制条件,请参见源。没有
担保甚至不是为了适销性或适合某一特定目的。
在第一种情况下,我希望获得通过/失败,无论系统是否与这些版本号完全匹配,在第二种情况下,我希望获得通过/失败,无论系统提供的版本号是否最低(=更大,相对于1.2.2比1.1.3更新)

因此,我想要实现的是:

  • 步骤1:识别3个数字点分隔的版本号(也允许像123.22.1这样的构造)
  • 第2步:将这个字符串分成3个部分,主要部分、次要部分和错误修复部分
之后,我可以将这些字符串值与预期值进行比较,并返回结果


有没有使用awk或类似工具的简单解决方案?

这里有一种方法,主要是在bash(版本>4.2)中使用shell函数实现

这不会返回最终的状态代码,只是向标准输出一个报告

#!/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}')

检查,使用
sort-V
您甚至不需要麻烦地拆分版本(您仍然需要从工具的输出中提取版本,但这可以通过一两个
grep
轻松完成,最后一个使用
-o
)这不是您希望在运行时解决的问题;这是一个您在安装时使用软件包管理器、虚拟环境等解决的问题,这样您的脚本就可以在假定有正确版本可用的环境中运行。我建议不要重新发明轮子,而使用
autoconf
,看看答案。
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