15

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.

CrispyDuck
  • 391
  • 1
  • 2
  • 8

5 Answers5

17

In bash, I was able to do this:

» export packversion=1.10.2
» export shortversion=${packversion%.*}
» printenv shortversion
1.10

From Bash Pocket Reference by Arnold Robbins (O'Reilly, 2016, 2nd edition):

${var%pattern} Use value of var after removing text matching pattern from the left. Remove the shortest matching piece.

Here, {.*} is the pattern to be removed, which corresponds to the patch version number.

spacewombat
  • 171
  • 1
  • 3
  • 3
    This is not ideal because if `packversion` is `1.10`, you end up with `shortversion` being `1`. OP probably wants to keep major and minor even if patch isn't specified. – Maros Apr 15 '20 at 08:13
12

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.

CrispyDuck
  • 391
  • 1
  • 2
  • 8
2

since the right answer here is hidden in one of the comments, thanks to Pascal:

cut -d '.' -f 1,2 <<< "$packversion"

bugkiller
  • 124
  • 3
0

something like this?

$ cat script.sh 
#!/bin/bash
read packversion major minor
echo "https://example.com/download/${major}.${minor}/src/${packversion}.tar.gz -O package.tar.gz"

Result:

$ bash script.sh
foobar 2 25 # Input
https://example.com/download/2.25/src/foobar.tar.gz -O package.tar.gz

Edit: Use this:

#!/bin/bash
read packversion
if [[ $packversion =~ [0-9]+\.[0-9]+ ]]; then
  version=${BASH_REMATCH[0]}
else
  echo "Looks like \"$packversion\" doesn't contain proper version" && exit 1
fi
echo "https://example.com/download/${version}/src/${packversion}.tar.gz -O package.tar.gz"

Result:

$ bash 123.sh
something-1.2.3-else
https://example.com/download/1.2/src/something-1.2.3-else.tar.gz -O package.tar.gz
Konstantin Vustin
  • 6,521
  • 2
  • 16
  • 32
0

If you want to extract all the version components individually (i.e., major, minor, and patch), you can do it like this:

#!/usr/bin/env bash

version=1.5.3

read major minor patch < <(echo $version | ( IFS=".$IFS" ; read a b c && echo $a $b $c ))

# Then you can use them like:
echo $major.$minor.$patch
Kactung
  • 690
  • 2
  • 8
  • 21