ehime
ehime

Reputation: 8375

Getting latest version from a Newline delimited list in BASH

I am running a small curl command to retrieve the following information from a webpage. I would like to receive from this output the "latest version" of whatever the damage file in question is. Things to keep in mind are that there are RC numbers, that this is also alread inside of a loop (code below) and that the the output might not always be in order.

Input

facter-1.7.5-rc1.dmg
facter-1.7.5-rc2.dmg
facter-1.7.5.dmg
facter-1.7.6.dmg
facter-2.0.1-rc1.dmg
facter-2.0.1-rc2.dmg
facter-2.0.1-rc3.dmg
facter-2.0.1-rc4.dmg
facter-2.0.1.dmg
facter-2.0.2.dmg
facter-2.1.0.dmg
facter-2.2.0.dmg
facter-2.3.0.dmg

Expected output

facter-2.3.0.dmg

Loop that is currently processing it

function get_latest()
{
  local url="$1"
  local name="$2"

  for pkg in $(wget -q "${url}" -O - |grep -o ">${name}.*dmg" |cut -c 2-); do 
    echo "${url}/${pkg}"; 
  done
}

FACTER_URL="$(get_latest 'http://downloads.puppetlabs.com/mac/' 'facter')"
echo $FACTER_URL ##http://downloads.puppetlabs.com/mac/facter-2.3.0.dmg

Upvotes: 2

Views: 26

Answers (2)

Gilles Quénot
Gilles Quénot

Reputation: 185055

Another approach (I use a proper XML/HTML parser) :

get_latest() {
  local url="$1"
  local name="$2"

  printf '%s\n' $(xmllint --html --xpath "//a/@href[contains(., '$2')]" $url) |
      sort -V |
      awk -F'"' 'END{print url$2}' url=$url
}

get_latest 'http://downloads.puppetlabs.com/mac/' 'facter'

Output :

http://downloads.puppetlabs.com/mac/facter-2.3.0.dmg

Note :

xmllint comes with libxml2-utils for debian and derivatives

Upvotes: 0

anubhava
anubhava

Reputation: 785068

You can use make use of sort -V here:

sort -rV file | head -1
facter-2.3.0.dmg

As per man sort:

  -V, --version-sort
          natural sort of (version) numbers within text

PS: This will also take care of rc versions.

Upvotes: 2

Related Questions