码迷,mamicode.com
首页 > 系统相关 > 详细

Linux之简单的shell编程

时间:2019-12-27 00:14:58      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:done   shell   while   http   素数   ima   ash   简单   prime   

分享几个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"

Linux之简单的shell编程

标签:done   shell   while   http   素数   ima   ash   简单   prime   

原文地址:https://www.cnblogs.com/ma1998/p/12105086.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!