原文链接,感谢原作者
功能:对用户从键盘输入的数字,进行排序,以1,2,3,4,5……升序格式输出,同时
不限定个数。
用到知识点:
比上篇多一个read和不定循环的实现
#!/bash/shell #Fuction:read different numbers and sort them #Date:2011 04 19 #Version:1.0.0.0 PATH=$PATH LANG=C export C i=0 var[1]=0 #1.input intergers while [ "$num" != "NO" ] do read -p "please input interger,else input NO to stop:" num let i++ var[i]=$num done let i-- #2.sort for (( a=1; a<=$i-1; a=a+1 )) do for (( j=1; j<=$i-a; j=j+1 )) do if [ "${var[$j]}" -gt "${var[$j+1]}" ]; then tmp="${var[$j]}" var[$j]="${var[$j+1]}" var[$j+1]="$tmp" fi done done #3.output result="" for ((a=1; a<=$i-1; a=a+1 )) do result="$result${var[$a]}," done echo "$result${var[$i]}" exit 0