在bash上排序版本字符串

use*_*991 2 linux bash shell

STRINGS.txt的示例内容:

    3.0.3
    3.0.11.2
    3.0.11.1
    3.0.11
    3.0.16
    3.0.15.1
    3.0.15
    3.0.14
    3.0.10.3
    3.0.10.2
    3.0.10.1
    3.0.13.1
    3.0.10
    3.0.13
    3.0.9
    3.0.12
    3.0.8
    3.0.7.2
    3.0.7.1
    3.0.7
    3.0.9.2
    3.0.9.1
    3.0.2
    3.0.8.1
    3.0.6.1
    3.0.6
    3.0.5
    3.0.1
    3.0.0
Run Code Online (Sandbox Code Playgroud)

是否可以仅使用bash对所有这些版本字符串进行排序,最新版本是否在顶部?

Cha*_*ffy 8

这是可能的,但却是一项愚蠢的工作.如果你有GNU排序:

sort -V -r <STRINGS.txt
Run Code Online (Sandbox Code Playgroud)

......将完全按照你的要求行事.


现在,如果你真的没有外部工具的意思,那么你就陷入了一些麻烦.Freenode的#bash IRC频道上的BlastHardcheese已经在本机bash中编写了以下快速排序算法,我为了便于阅读而修改了该算法,compare将可重放的函数分解出来,并使用Bash 4.3 namevars来处理可配置的变量名(当然,后一种变化意味着需要一个非常新版本的bash):

# this needs to be replaced for this particular case
compare(){
  (( $1 >= $2 ))
}

swap(){
  declare -n a=$1
  local t
  t=${a[$2]}
  a[$2]=${a[$3]}
  a[$3]=$t
}

partition(){
  declare -n a=$1
  local c p x
  p=${a[$4]}
  c=$2
  swap "$1" "$3" "$4"
  for((x=$2;x<$3;x++)); do
    if ! compare "${a[x]}" "$p"; then
      swap "$1" "$x" "$c"
      ((c++))
    fi
  done
  swap "$1" "$2" "$c"
  n=$c
}

quicksort(){
  declare -n a=$1
  (( "$2" >= "$3" )) && return
  local i n
  i=$((($2+$3)/2))
  partition "$1" "$2" "$3" "$i"
  quicksort "$1" "$2" "$((n-1))"
  quicksort "$1" "$((n+1))" "$3"
}
Run Code Online (Sandbox Code Playgroud)

...实现自己的比较功能,然后可以采用.

仅处理您在此处显示的案例:

# we want to return 0 if the first version is equal or later than the second
version_compare(){
  local -a first second

  # Let's start with trivial cases:
  if [[ $1 = "$2" ]] || [[ $1 = "$2".* ]]; then : "$1 >= $2"; return 0; fi

  IFS=. read -r -a first <<<"$1"
  IFS=. read -r -a second <<<"$2"

  local k
  for k in "${!first[@]}"; do
    local a=${first[$k]} b=${second[$k]}
    : "Evaluating field $k ($a vs $b)"
    if [[ ! $b ]]; then
      # ie. first=1.1.1, second=1.1; though this should have been handled above
      : "$1 >= $2"; return 0;
    fi
    if (( $b > $a )); then
      : "$1 < $2"; return 1;
    fi
  done

  : "$1 >= $2"; return 0;
}
compare() {
  version_compare "$2" "$1" # reverse sort order
}
Run Code Online (Sandbox Code Playgroud)

要执行文件IO,假设bash 4:

readarray -t versions <STRINGS.txt
quicksort versions 0 "$(( ${#versions[@]} - 1 ))"
printf '%s\n' "${versions[@]}"
Run Code Online (Sandbox Code Playgroud)