Linux之简单的shell编程
分享几个shell程序,便于linux期末复习
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"
相关推荐
tianhuak 2020-11-24
以梦为马不负韶华 2020-10-20
彼岸随笔 2020-10-20
yutou0 2020-10-17
applecarelte 2020-10-16
ourtimes 2020-10-16
woaimeinuo 2020-10-21
touchfuture 2020-09-16
laisean 2020-11-11
Julyth 2020-10-16
laisean 2020-09-27
flycappuccino 2020-09-27
liguojia 2020-09-27
huha 2020-10-16
lianshaohua 2020-09-23
zhangjie 2020-11-11
大牛牛 2020-10-30
firefaith 2020-10-30
liguojia 2020-10-20