這是我的一個氣泡代碼排序的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
我有兩個問題與此代碼:
- 輸入數組只需要3個數字
- ,然後我得到第42行錯誤說語法錯誤的命令,同時也
我已經試過while [] ; do
,但它不起作用。
它只是一天,我一直在嘗試bash語法。
你的腳本充滿語法錯誤。使用Shellscript,你必須照顧空間: while [1];做回聲再見;睡1;完成 是錯誤; while [1];做回聲再見;睡1;完成 這是正確 –
dAm2K
2012-03-18 10:22:23