-1

I have been struggling to define a variable value with sed, compare the value to "6.7" and then write to a file.

The value is a version so it's always an int or a double. However my code doesn't seem to work:

FILE="user/gradle-6.7-new"
VERSION=$(sed -n '/^distributionUrl=/s/.*gradle-\([^-]*\)-.*/\1/p' $FILE)
if [[ $VERSION > 6.7 ]]
then
  $VERSION > /tmp/version_file
fi

Thanks.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
jrz
  • 1,213
  • 4
  • 20
  • 54

2 Answers2

0

awk may be an easier approach.

#!/usr/bin/env bash
FILE="user/gradle-6.7-new"
awk -F- '{version=$2} version >= 6.7 { print version > "/tmp/version_file" }' <<< $FILE
HatLess
  • 10,622
  • 5
  • 14
  • 32
  • Thanks. The if statement seems to not be working though. nothing is being written into `/tmp/version_file` – jrz Dec 07 '21 at 20:44
  • Real problem is not with the `sed` command. The OP wants to check if `$VERSION` (which can be something like `6.9.8`) greater than `6.7`. – M. Nejat Aydin Dec 07 '21 at 20:52
  • @HatLess yes I also tested locally. @M.NejatAydin I am okay also with only "one dot version compare" like `6.7` and `6.9`. But this doesnt work for mw I tried with `FILE="user/gradle-6.9-new"` I expect `6.9` to be printed into /tmp/version_file but nothing gets is in there – jrz Dec 07 '21 at 21:00
  • @M.NejatAydin Thanks, I totally missed that – HatLess Dec 07 '21 at 21:03
  • @HatLess the output I get now is `6.7/tmp/version_file` – jrz Dec 07 '21 at 21:07
  • @I.zv That would imply you removed the redirect `>` from the print statement? – HatLess Dec 07 '21 at 21:11
  • @HatLess I literally copy and pasted your answer. Could you share what you run and get locally? – jrz Dec 07 '21 at 21:12
  • @I.zv `awk -F- '{version=$2} version >= 6.7 { print version > "/tmp/version_file" }' <<< $FILE` `cat /tmp/version_file` `6.7` – HatLess Dec 07 '21 at 21:15
0

Assumptions:

  • OP's current code properly populates VERSION (ie, I'm assuming OP is not having problems populating VERSION from $FILE)

I'd recommend reviewing the answers @ this link.

While not chosen as the accepted answer, a variation on this answer is relatively straight forward, ie, let sort/-V order the two version strings to be compared and then test the first (or last) line of the sort/-V output accordingly, eg:

testVERSION='6.7'

for VERSION in 6.6 6.7 6.7.0 6.7.1 6.71
do
    op='<'
    if [[ $(printf "%s\n%s\n" "${testVERSION}" "${VERSION}" | sort -V | head -1) == "${testVERSION}" ]]
    then
        op='='    # at this point we know ${VERSION} >= ${testVERSION}, but assuming OP needs to distinguish between '>=' and '>' we need one more test ...

        [[ "${VERSION}" != "${testVERSION}" ]] && op='>'
    fi
    echo "${VERSION} ${op} ${testVERSION}"
done

This generates:

6.6 < 6.7
6.7 = 6.7
6.7.0 > 6.7
6.7.1 > 6.7
6.71 > 6.7

From here OP should be able to adapt the logic to generate the desired result, eg:

testVERSION='6.7'

[[ $(printf "%s\n%s\n" "${testVERSION}" "${VERSION}" | sort -V | head -1) == "${testVERSION}" ]] &&
[[ "${VERSION}" != "${testVERSION}" ]] &&
echo "$VERSION" > /tmp/version_file           # we get this far if ${VERSION} > ${testVERSION}
markp-fuso
  • 28,790
  • 4
  • 16
  • 36