2012-03-18 62 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命令,所以空間是圍繞[]需要與許多其他編程語言。