I am trying to compare version in windows terminal. It works greate in Git Bash but in windows terminal it shows gibrish and wrong result?
verlte() {
[ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
}
verlt() {
[ "$1" = "$2" ] && return 1 || verlte $1 $2
}
verlte 2.5.7 2.5.6 && echo "yes" || echo "no" # no
verlt 2.4.10 2.4.9 && echo "yes" || echo "no" # no
verlt 2.4.8 2.4.10 && echo "yes" || echo "no" # yes
verlte 2.5.6 2.5.6 && echo "yes" || echo "no" # yes
verlt 2.5.6 2.5.6 && echo "yes" || echo "no" # no
Result in windows terminal
Result in Git Bash
CodePudding user response:
Borrowing from Sort a list that contains version numbers properly, which references How to sort by file name the same way Windows Explorer does?, here's my attempt at converting to PowerShell syntax:
# compare two arbitrary version strings using natural sorting
# allows strings to contain non-numeric components
function verlte {
param(
[string]$v1,
[string]$v2
)
function ToNatural {
param( [string]$v )
[regex]::Replace($v, '\d ', { $args[0].Value.PadLeft(20) })
}
(ToNatural $v1) -le (ToNatural $v2)
}
function verlt {
param(
[string]$v1,
[string]$v2
)
if ($v1 -eq $v2) { return $false }
verlte $v1 $v2
}
verlte 2.5.7 2.5.6 # False
verlt 2.4.10 2.4.9 # False
verlt 2.4.8 2.4.10 # True
verlte 2.5.6 2.5.6 # True
verlt 2.5.6 2.5.6 # False
CodePudding user response:
It seems you're comparing versions. In PowerShell and .NET framework there are already [version]
to do that:
PS C:\Users> [version]"2.5.7" -le [version]"2.5.6"
False
PS C:\Users> [version]"2.4.10" -lt [version]"2.4.9"
False
PS C:\Users> [version]::new(2, 4, 8) -lt [version]::new(2, 4, 10)
True
PS C:\Users> [version]"2.5.6" -le [version]"2.5.6"
True
PS C:\Users> [version]::new(2, 5, 6) -lt [version]::new(2, 5, 6)
False
See PowerShell Comparison Operators for more information