【问题标题】:Using awk to extract gcc version使用awk提取gcc版本
【发布时间】:2021-09-19 05:52:30
【问题描述】:

我想用awk命令获取gcc版本,但是返回的字符串是空的。

$ gcc --version
gcc (GCC) 11.1.0
Copyright (C) 2021 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.
$ gcc --version | head -1 | awk '{for(i=1;i<=NF;i++){ if(match($i,/^[0-9]\.[0-9]\.[0-9]$/))  {print $i; exit 0}}}'
$

不知道为什么会失败。有什么想法吗?

【问题讨论】:

    标签: gcc awk version


    【解决方案1】:
    gcc -dumpfullversion
    

    输出:

    9.3.0

    【讨论】:

      【解决方案2】:

      您只检查一位数字。

      只允许多个数字,它必须工作,像这样:

      awk '{for(i=1;i<=NF;i++){ if(match($i,/^[0-9]{1,2}\.[0-9]*\.[0-9]*$/))  {print $i; exit 0}}}'
      

      gcc 还有一个短版本选项:

      -转储版本

      所以你不需要 awk :)

      【讨论】:

        【解决方案3】:
        gcc --version |  awk '{ print $NF;exit }'
        

        输出(在我的系统上):

        $ gcc --version
        gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
        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.
        
        $ gcc --version |  awk '{ print $NF;exit }'
        9.3.0
        $
        

        或者:

        gcc -dumpspecs | grep -A1 version | tail -1
        

        【讨论】:

          猜你喜欢
          • 2020-01-22
          • 1970-01-01
          • 2020-09-26
          • 1970-01-01
          • 2014-09-21
          • 2019-06-25
          • 2015-09-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多