0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

バージョン番号値を比較(シェルスクリプト)

Last updated at Posted at 2022-12-19

こんにちは。
二つのバージョン番号の値を比較するシェルスクリプトを作りました1bc コマンドで数値比較させています。

  • 対応するバージョン番号は、xx.yy.zz という形式で、各2桁以下とし、また xx.yy や xx でも対応します。
$ echo "3.0.1 > 3" | ./bc_ver.sh 
true
bc_ver.sh
#!/bin/sh

## functions
bc_f () {
  [ "$(bc)" -eq 1 ]   # "222222 > 111111" => true, for example
}

ver2int_f () {
  echo "$1" | awk -F. '{printf "%2d%02d%02d", $1,$2,$3}'  # xx.yy.zz => xxyyzz
}

convert_f () {
  set $(cat)
  echo "$(ver2int_f "$1")" "$2" "$(ver2int_f "$3")"
}

split_f () {
  perl -pe 's/([\d\.]+).*?([=<>]+).*?([\d\.]+)/$1 $2 $3/g'  # "xx<yy" => "xx < yy", for example
}

## main
[ -p /dev/stdin ] && INPUT=$(cat) || INPUT="$@"
echo "$INPUT" | split_f | convert_f | bc_f && echo true || echo false

exit

  1. 参考:「シェルスクリプトやmakefileでのバージョン番号 x.y.z の比較

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?