这是我的一个气泡代码排序的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