如何比较两个字符串的点分隔版本格式在巴什?

如何比较两个字符串的点分隔版本格式在巴什?

有没有办法比较bash上的这些字符串,例如:2.4.52.82.4.5.1?


临摹微笑
浏览 433回答 3
3回答

UYOU

以下是一个纯Bash版本,它不需要任何外部实用程序:#!/bin/bashvercomp () {&nbsp; &nbsp; if [[ $1 == $2 ]]&nbsp; &nbsp; then&nbsp; &nbsp; &nbsp; &nbsp; return 0&nbsp; &nbsp; fi&nbsp; &nbsp; local IFS=.&nbsp; &nbsp; local i ver1=($1) ver2=($2)&nbsp; &nbsp; # fill empty fields in ver1 with zeros&nbsp; &nbsp; for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))&nbsp; &nbsp; do&nbsp; &nbsp; &nbsp; &nbsp; ver1[i]=0&nbsp; &nbsp; done&nbsp; &nbsp; for ((i=0; i<${#ver1[@]}; i++))&nbsp; &nbsp; do&nbsp; &nbsp; &nbsp; &nbsp; if [[ -z ${ver2[i]} ]]&nbsp; &nbsp; &nbsp; &nbsp; then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # fill empty fields in ver2 with zeros&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ver2[i]=0&nbsp; &nbsp; &nbsp; &nbsp; fi&nbsp; &nbsp; &nbsp; &nbsp; if ((10#${ver1[i]} > 10#${ver2[i]}))&nbsp; &nbsp; &nbsp; &nbsp; then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1&nbsp; &nbsp; &nbsp; &nbsp; fi&nbsp; &nbsp; &nbsp; &nbsp; if ((10#${ver1[i]} < 10#${ver2[i]}))&nbsp; &nbsp; &nbsp; &nbsp; then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 2&nbsp; &nbsp; &nbsp; &nbsp; fi&nbsp; &nbsp; done&nbsp; &nbsp; return 0}testvercomp () {&nbsp; &nbsp; vercomp $1 $2&nbsp; &nbsp; case $? in&nbsp; &nbsp; &nbsp; &nbsp; 0) op='=';;&nbsp; &nbsp; &nbsp; &nbsp; 1) op='>';;&nbsp; &nbsp; &nbsp; &nbsp; 2) op='<';;&nbsp; &nbsp; esac&nbsp; &nbsp; if [[ $op != $3 ]]&nbsp; &nbsp; then&nbsp; &nbsp; &nbsp; &nbsp; echo "FAIL: Expected '$3', Actual '$op', Arg1 '$1', Arg2 '$2'"&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; echo "Pass: '$1 $op $2'"&nbsp; &nbsp; fi}# Run tests# argument table format:# testarg1&nbsp; &nbsp;testarg2&nbsp; &nbsp; &nbsp;expected_relationshipecho "The following tests should pass"while read -r testdo&nbsp; &nbsp; testvercomp $testdone << EOF1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =2.1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2.2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <3.0.4.10&nbsp; &nbsp; &nbsp;3.0.4.2&nbsp; &nbsp; &nbsp; >4.08&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4.08.01&nbsp; &nbsp; &nbsp; <3.2.1.9.8144 3.2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; >3.2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3.2.1.9.8144 <1.2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2.1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <2.1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; >5.6.7&nbsp; &nbsp; &nbsp; &nbsp; 5.6.7&nbsp; &nbsp; &nbsp; &nbsp; =1.01.1&nbsp; &nbsp; &nbsp; &nbsp;1.1.1&nbsp; &nbsp; &nbsp; &nbsp; =1.1.1&nbsp; &nbsp; &nbsp; &nbsp; 1.01.1&nbsp; &nbsp; &nbsp; &nbsp;=1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =1.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =1.0.2.0&nbsp; &nbsp; &nbsp; 1.0.2&nbsp; &nbsp; &nbsp; &nbsp; =1..0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =1.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1..0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=EOFecho "The following test should fail (test the tester)"testvercomp 1 1 '>'运行测试:$ . ./vercompThe following tests should passPass: '1 = 1'Pass: '2.1 < 2.2'Pass: '3.0.4.10 > 3.0.4.2'Pass: '4.08 < 4.08.01'Pass: '3.2.1.9.8144 > 3.2'Pass: '3.2 < 3.2.1.9.8144'Pass: '1.2 < 2.1'Pass: '2.1 > 1.2'Pass: '5.6.7 = 5.6.7'Pass: '1.01.1 = 1.1.1'Pass: '1.1.1 = 1.01.1'Pass: '1 = 1.0'Pass: '1.0 = 1'Pass: '1.0.2.0 = 1.0.2'Pass: '1..0 = 1.0'Pass: '1.0 = 1..0'The following test should fail (test the tester)FAIL: Expected '>', Actual '=', Arg1 '1', Arg2 '1'

慕盖茨4494581

如果您有coreutils-7(在Ubuntu业力,但不是黄疸),那么您sort命令应该有一个-V选项(版本排序),您可以使用该选项进行比较:verlte() {&nbsp; &nbsp; [&nbsp; "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]}verlt() {&nbsp; &nbsp; [ "$1" = "$2" ] && return 1 || verlte $1 $2}verlte 2.5.7 2.5.6 && echo "yes" || echo "no" # noverlt 2.4.10 2.4.9 && echo "yes" || echo "no" # noverlt 2.4.8 2.4.10 && echo "yes" || echo "no" # yesverlte 2.5.6 2.5.6 && echo "yes" || echo "no" # yesverlt 2.5.6 2.5.6 && echo "yes" || echo "no" # no

慕森王

可能没有普遍正确的方法来实现这一点。如果要比较debian包系统中的版本,请尝试dpkg --compare-versions <first> <relation> <second>.
打开App,查看更多内容
随时随地看视频慕课网APP