分享几个shell程序,便于linux期末复习
1.判断用户输入的数是否为回文数
1 #!/bin/bash
2 read in
3 res=`echo $in|rev`
4 if [ $res -eq $in ]
5 then
6 echo "$in is a huiwenshu!"
7 elif [ $res -ne $in ]
8 then
9 echo "$in not is a huiwenshu"
10 fi
2.计算用户输入的一个数的阶乘
1 #!/bin/bash 2 sum=1 3 i=1 4 read n 5 while [ $i -le $n ] 6 do 7 sum=$[$sum*$i] 8 i=$[$i+1] 9 done 10 echo "sum=$sum"
3.判断用户输入的数是否为素数
1 #!/bin/bash 2 read num 3 declare -i count=0 4 for n in `seq 1 $num` 5 do 6 if [ $((num%n)) -eq 0 ] 7 then 8 count=$[$count+1] 9 fi 10 done 11 if [ $count -eq 2 ] 12 then 13 echo "$num is a prime num" 14 else echo "$num not is a prime num" 15 fi
4.计算斐不那楔数列的的前n项和
1 #!/bin/bash 2 read num 3 a=1 4 b=1 5 c=0 6 sum=0 7 for((i=0;i<num;i++)) 8 do 9 echo "$a" 10 let sum+=a 11 let c=a+b 12 let a=b 13 let b=c 14 done 15 echo "sum=$sum"