1. 编写一个shell程序,将输入的十进制整数,转换成N进制数并输出。(假设N在2到9之间)。
#!/bin/bash
echo -n "Please input a decimalism base:"
read a
echo -n "Please input N:"
read b
if [ "$b" -lt 2 -o "$b" -gt 9 ]
then echo "ERROR INPUT"
exit
fi
i=0
sum[0]=0
sum[$i]=$(($a%$b))
((a=$a/$b))
while [ "$a" -gt 0 ]
do
((i=$i+1))
sum[$i]=$(($a%$b))
((a=$a/$b))
done
while [ "$i" -ge 0 ]
do
echo -n "${sum[$i]} "
((i=$i-1))
done
echo " "
echo "BYE!"
2. 编写一个shell程序,用递归法计算整数n的阶乘。
#!/bin/bash
echo -n "Please into your number:"
read num
result=$num;
while [ "$num" -gt 0 ]
do
if [ "$num" -eq 1 ]
then
break;
else
((result=$result*(($num-1))))
((num=$num-1))
fi
done
echo "$result"
3. 编写一个shell程序,任意输入10个整数,用冒泡法按由小到大排序。
#!/bin/bash
i=0
n=10
while [ "$i" -lt "$n" ]
do
echo -n "Please input No.$(($i+1)) number:"
read num[$i]
((i=$i+1))
done
i=0
while [ "$i" -lt "$n" ]
do
j=0
while [ "$j" -lt "$(