从bash

时间:2017-10-28 23:56:46

标签: arrays bash shell sorting

所以我有以下bash脚本,它应该读取n个整数的.txt文件,将它添加到数组中,使用选择排序对数组进行排序,然后打印数组:

a=()
filename="$1"
while IFS='' read -r line || [[ -n "$line" ]]; do
    a+=($line)
done < "$filename"

for((i=0; i<${#a[@]}; i++))
do
  min=$i
  for((j=$i+1; j<${#a[@]}; j++))
  do
    if (( ${a[$j]} <= ${a[$min]} ))
    then
      $min=$j
      echo "$min"
    fi
  done
  temp=a[$i]
  a[$i]=a[$min]
  a[$min]=$temp
done

for i in ${a[@]}
do
  echo $i
done

问题是内部循环中的条件会引发异常,我认为问题是数组元素都是字符串,我不知道在这种情况下如何将它们转换为字符串。另外,我认为我没有正确地交换这两个值。输出:

sh selectionsort.sh 10.txt
 ") 152ax error: invalid arithmetic operator (error token is "
 ") 152ax error: invalid arithmetic operator (error token is "
 ") 152ax error: invalid arithmetic operator (error token is "
 ") 152ax error: invalid arithmetic operator (error token is "
 ") 152ax error: invalid arithmetic operator (error token is "
 ") 152ax error: invalid arithmetic operator (error token is "
 ") 152ax error: invalid arithmetic operator (error token is "
 ") 152ax error: invalid arithmetic operator (error token is "
 ") 152ax error: invalid arithmetic operator (error token is "
 ") 436ax error: invalid arithmetic operator (error token is "
 ") 436ax error: invalid arithmetic operator (error token is "
 ") 436ax error: invalid arithmetic operator (error token is "
 ") 436ax error: invalid arithmetic operator (error token is "
 ") 436ax error: invalid arithmetic operator (error token is "
 ") 436ax error: invalid arithmetic operator (error token is "
 ") 436ax error: invalid arithmetic operator (error token is "
 ") 436ax error: invalid arithmetic operator (error token is "
 ") 756ax error: invalid arithmetic operator (error token is "
 ") 756ax error: invalid arithmetic operator (error token is "
 ") 756ax error: invalid arithmetic operator (error token is "
 ") 756ax error: invalid arithmetic operator (error token is "
 ") 756ax error: invalid arithmetic operator (error token is "
 ") 756ax error: invalid arithmetic operator (error token is "
 ") 756ax error: invalid arithmetic operator (error token is "
 ") 391ax error: invalid arithmetic operator (error token is "
 ") 391ax error: invalid arithmetic operator (error token is "
 ") 391ax error: invalid arithmetic operator (error token is "
 ") 391ax error: invalid arithmetic operator (error token is "
 ") 391ax error: invalid arithmetic operator (error token is "
 ") 391ax error: invalid arithmetic operator (error token is "
 ") 435ax error: invalid arithmetic operator (error token is "
 ") 435ax error: invalid arithmetic operator (error token is "
 ") 435ax error: invalid arithmetic operator (error token is "
 ") 435ax error: invalid arithmetic operator (error token is "
 ") 435ax error: invalid arithmetic operator (error token is "
 ") 404ax error: invalid arithmetic operator (error token is "
 ") 404ax error: invalid arithmetic operator (error token is "
 ") 404ax error: invalid arithmetic operator (error token is "
 ") 404ax error: invalid arithmetic operator (error token is "
 ") 853ax error: invalid arithmetic operator (error token is "
 ") 853ax error: invalid arithmetic operator (error token is "
 ") 853ax error: invalid arithmetic operator (error token is "
 ") 278ax error: invalid arithmetic operator (error token is "
 ") 278ax error: invalid arithmetic operator (error token is "
 ") 643ax error: invalid arithmetic operator (error token is "
a[0]
a[1]
a[2]
a[3]
a[4]
a[5]
a[6]
a[7]
a[8]
a[9]

任何帮助表示赞赏! 注意:我知道有更简单的方法对数组进行排序,我正在使用这个脚本来实现选择排序。

2 个答案:

答案 0 :(得分:1)

如果您使用 bash ,则可以利用其内置函数,例如sortreadarray。 (注意readarray可用,因为 bash 4 +,默认情况下没有 OS X 支持。)

您可以删除排序代码并使用以下here

所述的代码
readarray -t sorted < <(for b in "${a[@]}"; do echo "$b"; done | sort)

$a将转换为已排序的数组。

答案 1 :(得分:1)

$min=$j绝对是一个错误。它应该是min=$j。您访问数组单元格的方式(temp=a[$i]a[$i]=a[$min])也是如此。请改用temp=${a[$i]}a[$i]=${a[$min]}。但是还有一些你可以改进的事情:

declare -ai a=()
declare -i i j min temp
declare line

filename="$1"
while IFS='' read -r line; do
  [[ $line =~ ^[0-9]+$ ]] && a+=($line) || printf "Warning: not a number (%s)\n" "$line"
done < "$filename"

for (( i=0; i<${#a[@]}; i++ ))
do
  min=$i
  for (( j=i+1; j<${#a[@]}; j++ ))
  do
    if (( a[j] <= a[min] ))
    then
      min=$j
      echo "$min"
    fi
  done
  temp=${a[i]}
  a[i]=${a[min]}
  a[min]=$temp
done

for i in "${a[@]}"
do
  echo "$i"
done

在算术评估(((...)))和数组索引(a[...])的上下文中,变量名称被解释为其数值(不需要$)。

我还添加了变量声明和测试以消除非整数条目。