如何从makefile中的字符串中隔离版本号?

时间:2019-07-15 19:50:34

标签: shell makefile

我想从字符串“ Version:1.58.0.1ubuntu1”或通常任何其他Boost版本中获取Boost库版本号1.58。这样我就可以将boost的当前版本与我需要匹配的特定版本进行比较。这就是我到目前为止所拥有的。

Configure:
    if ( test  -d $(Boost) )
    then
            CurrVer=$$(dpkg -s libboost-dev | grep 'Version'
            echo $$CurrVer
            echo $$CurrVer  | tr -cd [:digit:]
    else
            make DLBoostV1_58
    fi

问题是我可以将字符串缩小到数字158011,但我不知道如何删除数字011。

我已经阅读了geeksforgeeks网站上的grep,sed和awk命令,但到现在为止,我是How to extract numbers from a string?https://askubuntu.com/questions/147474/how-can-i-find-boost-versionhttps://www.geeksforgeeks.org/tr-command-in-unix-linux-with-examples/nd

预期输出:158 结果输出:158011

2 个答案:

答案 0 :(得分:0)

假设您已在Shell变量$('#submit').click(function () { var data = { SupplierId: $('#supplierId').val().trim(), PartNumber: $('#partNumber').val().trim(), PartDescription: $('#partDescription').val().trim(), PartFiles: ????? } $.ajax({ url: '/Parts/SaveNewPart', type: "POST", data: JSON.stringify(data), dataType: "JSON", contentType: "application/json", success: function (d) { //check is successfully save to database if (d.status == true) { alert('Successfully done.'); 中捕获了Boost版本消息,则可以使用参数扩展的prefix-removal选项删除开头文本(CurrVer删除所有内容,直到最后一个空格字符),在数组分配(${CurrVer##* })内,以句点(。)作为字段分隔符(a=(...)),以在右侧定界符处分割版本字符串。然后,您只需要读回所需的数组元素即可。

例如,

IFS=.

ShortVer=$( IFS=.; a=(${CurrVer##* }); echo "${a[0]}.${a[1]}" ) 的内容转义为练习。

答案 1 :(得分:0)

您可以使用gmtt,每个glob-match可以分割字符串:

include gmtt-master/gmtt-master/gmtt.mk

VERSION := Version: 1.58.0.1ubuntu1
VERS-AS-LIST := $(call glob-match,$(VERSION),Version: *.*.*.*ubuntu*)

$(info $(VERS-AS-LIST))

MAJOR := $(word 2,$(VERS-AS-LIST))
MINOR := $(word 4,$(VERS-AS-LIST))
BUGFIX := $(word 6,$(VERS-AS-LIST))

$(info $(MAJOR)-$(MINOR)-$(BUGFIX))

输出:

$ make
Version:§ 1 . 58 . 0 . 1 ubuntu 1
1-58-0

请注意,全局模式的第一部分是Version:,末尾有一个空格字符,该字符通过§字符(包含在gmtt变量$(-spacereplace)中是可保留的,并且可移动)功能$(call spc-unmask,_string_)