|
要求:
A shell script which prints the given numerical arguments in descending order.
源码:
#!/bin/bash
total=$#
if [ $total -lt 1 ]
then
echo "Usage: $0 arug1 ..."
else
cou=0
while [ $1 ]
do
A[$cou]=$1
cou=`expr $cou + 1`
shift
done
i=0
j=0
iend=`expr $total - 1`
jend=$total
while [ $i -lt $iend ]
do
j=`expr $i + 1`
while [ $j -lt $jend ]
do
if [ ${A[$i]} -lt ${A[$j]} ]
then
tmp=${A[$i]}
A[$i]=${A[$j]}
A[$j]=$tmp
fi
j=`expr $j + 1`
done
i=`expr $i + 1`
done
cou=0
while [ $cou -lt $total ]
do
echo ${A[$cou]}
cou=`expr $cou + 1`
done
fi
运行:
e0300481@shell:~/scripts$ . Q15 9 3 4 2 7 0
9
7
4
3
2
0
e0300481@shell:~/scripts$
第一次用bash写这么“长”的程序,请大家多多指点:p |
|