-1

I want to obtain the Boost Library Version number 1.58 out of the string "Version: 1.58.0.1ubuntu1" or generally any other version of Boost. This would allow me to compare the current version of boost to my specific version that I need to match. This is what I have so far.

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

The problem is that I can narrow the string down to the digits 158011 but I can't figure out how to remove the digits 011.

I have read geeksforgeeks website for grep, sed, and awk commands but what got me to this point are, How to extract numbers from a string?, https://askubuntu.com/questions/147474/how-can-i-find-boost-version, https://www.geeksforgeeks.org/tr-command-in-unix-linux-with-examples/nd .

Expecting output: 158 Resulting output: 158011

Tony Rom
  • 3
  • 4

2 Answers2

0

Supposing you have captured the Boost version message in shell variable CurrVer, you can use the prefix-removal option of parameter expansion to remove the lead text (${CurrVer##* } removes everything up to the last space character), inside an array assignment (a=(...)) with the period (.) as a field separator (IFS=.) to split the version string at the right delimiters. Then you just need to read back the array elements you want.

For example,

ShortVer=$( IFS=.; a=(${CurrVer##* }); echo "${a[0]}.${a[1]}" )

Escaping that for make is left as an exercise.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

You can use gmtt which can take apart strings per 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))

Output:

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

Notice that the first part of the glob pattern was Version: with a space character at the end, which is conserved via the § character (contained in the gmtt variable $(-spacereplace) and removable with the function $(call spc-unmask,_string_))

Vroomfondel
  • 2,704
  • 1
  • 15
  • 29
  • From my understanding, the string looks to be hard coded in. I can see that I could possibly changed what variable Version is set to but in the next line, what if it was not ubuntu? – Tony Rom Jul 17 '19 at 18:19
  • @TonyRom the strings are hardcoded just for demanstration. You can replace that with e.g. `VERSION := $(shell dpkg -s libboost-dev)`. – Vroomfondel Aug 02 '19 at 10:05
  • @TonyRom For your second point: what is the general format for the version string? `glob-match` will yield the empty string if there is no match, thus you could distinguish `ubuntu` from e.g. `gentoo` etc. by putting them in different variable which will all be empty except for the right one. Or you just pick the distribution name as discriminator and continue with `ifeq` etc. – Vroomfondel Aug 02 '19 at 10:11