Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH get major/minor version from string

I am quite new BASH scripting and I am trying to create my own script to download/build from source a package. However I would like to add some interaction to my script to make it future ready for newer versions.

My script asks the user for the version they would like to install and generates the wget command and download link. See excerpt below:

# Ask user for required version
read packversion

# Download version from internet (depending on version provided above)
wget https://example.com/download/1.10/src/$packversion.tar.gz -O package.tar.gz

This works OK, however if you look at the url it contains 1.10, I would like to pull just the major/minor version from the provided string and pre-populate that also.

Is there a function I can use to achieve this?

Thanks in advance.

like image 597
CrispyDuck Avatar asked Mar 13 '18 09:03

CrispyDuck


1 Answers

I have managed to achieve what I was after by doing the following:

shortversion="$(cut -d '.' -f 1 <<< "$packversion")"."$(cut -d '.' -f 2 <<< "$packversion")"

This then creates a new variable which I can use to create the url.

wget https://example.com/download/$shortversion/src/package-$packversion.tar.gz

Though not sure if its the best way, works for me though.

like image 157
CrispyDuck Avatar answered Oct 26 '22 11:10

CrispyDuck