Linux之简单的shell编程

Sabrina 2019-12-27

分享几个shell程序,便于linux期末复习

 Linux之简单的shell编程

1.判断用户输入的数是否为回文数

#!/bin/bash
read in
res=`echo $in|rev`
if [ $res -eq $in ]
then
        echo "$in is a huiwenshu!"
elif [ $res -ne $in ]
then
        echo "$in not is a huiwenshu"
fi

 2.计算用户输入的一个数的阶乘

#!/bin/bash
sum=1
i=1
read n
while [ $i -le $n ]
do
sum=$[$sum*$i]
i=$[$i+1]
done
echo "sum=$sum"

3.判断用户输入的数是否为素数

#!/bin/bash
read num
declare -i count=0
for n in `seq 1 $num`
do
        if [ $((num%n)) -eq 0 ]
        then
        count=$[$count+1]
        fi
done
if [ $count -eq 2 ]
then
        echo "$num is a prime num"
else echo "$num not is a prime num"
fi

4.计算斐不那楔数列的的前n项和

#!/bin/bash
read num
a=1           
b=1
c=0 
sum=0
for((i=0;i<num;i++))
do
        echo "$a"
        let sum+=a
        let c=a+b 
        let a=b 
        let b=c 
done
echo "sum=$sum"

相关推荐