Sorting not working correctly in bash

  • A+
Category:Languages

I have a variable VarExp with following 2 values

1.5.2 1.5.3 

I have another variable VarCurr with following 1 value

1.8.1 

I want to compare VarCurr with VarExp and want to echo SUCCESS only when

VarCurr >= VarExp 

I have written the following code but it it always returning FAILURE

VarExp='1.5.2 1.5.3' VarCurr='1.8.1'  printf -v versions '%s/n%s' "$VarExp" "$VarCurr" if [[ $versions = "$(sort -V <<< "$versions")" ]]; then     echo 'FAILURE' else     echo 'SUCCESS' fi 

VarCurr need to be >= the lowest value contained in VarExp

 


With GNU sort for -V:

$ cat tst.sh #!/bin/bash varExp='1.5.2 1.5.3' varCurr=$1  minVarExp=$(printf '%s/n' $varExp | sort -V | head -1) maxOfVers=$(printf '%s/n' "$minVarExp" "$varCurr" | sort -V | tail -1)  if [[ $maxOfVers = $varCurr ]]; then     echo 'SUCCESS' else     echo 'FAILURE' fi  $ ./tst.sh 1.8.1 SUCCESS  $ ./tst.sh 1.5.1 FAILURE  $ ./tst.sh 1.5.2 SUCCESS 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: