2012-03-18 65 views
1

这是我的一个气泡代码排序的n个数:语法错误做

#!/bin/bash 

echo -n "Input n, the number of numbers" 
read N 
declare -a array[N] 
echo -e "Input the elements, press enter after each element" 
for i in seq 1 $N 
do 
    read array[$i] 
done 

swap1() 
{ # for swapping two numbers, we are employing bubble sort 
    local temp = ${array[$1]} 
    array[$1] = ${array[$2]} 
    array[$2]=$temp 
    return 
} 

numb_elements=${#array[@]} 
let "comparisons = $numb_elements - 1" 

count=1 

while [ "$comparisons" -gt 0] 
do 
    index =0 

    while[ "$index" -lt "$comparisons" ];do 
     if [ ${array[$index]} \> ${array[ 'expr $index + 1']} ] 
     then 
     swap1 $index 'expr $index + 1' 
     fi 
     let "index += 1" # Or, index+=1 on Bash, ver. 2.1 or newer 
    done 

    let "comparisons -=1" 
    echo 
    echo "$count: ${array[@]} 
    echo 

    let "count +=1" 
done 
exit 0 

我有两个问题与此代码:

  1. 输入数组只需要3个数字
  2. ,然后我得到第42行错误说语法错误的命令,同时也

我已经试过while [] ; do,但它不起作用。

它只是一天,我一直在尝试bash语法。

+0

你的脚本充满语法错误。使用Shellscript,你必须照顾空间: while [1];做回声再见;睡1;完成是错误; while [1];做回声再见;睡1;完成这是正确 – dAm2K 2012-03-18 10:22:23

回答

1

你犯了一系列的错误:

  • 正确的空间是根本shell脚本
  • 缺少``顶点执行代码,并得到输出
  • 逻辑错误(开始从第二阵列插入元件和使用它从第一个)
  • 迭代错误的数目的时间冒泡ALG

这是您的代码更正。

#!/bin/bash 

swap1() { # for swapping two numbers, we are employing bubble sort 
     local temp=${array[$1]} 

     array[$1]=${array[$2]} 
     array[$2]=$temp 
     return 
} 


echo -n "Input n, the number of numbers: " 

read N 
declare -a array[$N] 

echo -e "Input the elements, press enter after each element" 
for i in `seq 1 $N` 
do 
     read array[$i] 
done 

numb_elements=${#array[@]} 
#let "comparisons = $numb_elements - 1" 
comparisons=$numb_elements 
count=1 
while [ "$comparisons" -gt 0 ] 
do 
     index=1 
     while [ "$index" -lt "$comparisons" ] 
     do 
       tmp=`expr $index + 1` 
       if [ ${array[$index]} -gt ${array[$tmp]} ] 
       then 
         swap1 $index $tmp 
       fi 
       let "index += 1" # Or, index+=1 on Bash, ver. 2.1 or newer 
     done 
     let "comparisons -= 1" 
     echo 
     echo "$count: ${array[@]}" 
     echo 
     let "count += 1" 
done 

exit 0 
+0

错误粘贴[while($比较“-gt 0”表示整数表达预期 – simplycurious 2012-03-18 11:10:07

+0

numb_elements = $ {#array [@] } #let“comparisons = $ numb_elements - 1” comparisons = $ numb_elements – simplycurious 2012-03-18 11:10:35

+0

就是上面的权利吗?“{#”??? – simplycurious 2012-03-18 11:10:53

1

尝试这种情况:

while [ "$comparisons" -gt 0] 

应为(右括号]之前通知空间):

while [ "$comparisons" -gt 0 ] 
2

而且不写

for i in seq 1 $N 

其中迭代我超过了设定的{“seq”,“1”,$ N},但输入

for i in $(seq 1 $N) 

插入命令的结果作为代码的一部分。

你忘了在这条线收盘报价:

echo "$count: ${array[@]} 

还嵌套循环的代码很厉害缩进,所以这是一个有点难以阅读和调试。

+0

是的我知道坏的缩进,在第34行,在while循环[编辑] – simplycurious 2012-03-18 10:29:35

2

到目前为止,我已经找到了以下错误:

while [ "$comparisons" -gt 0 ] 
          ^missing space here 

while [ "$index" -lt "$comparisons" ];do 
    ^missing space 

echo "$count: ${array[@]}" 
         ^missing quote 

注意,在bash [相当于test命令,所以空间是围绕[]需要与许多其他编程语言。