Linux之簡單的shell編程

分享幾個shell程序,便於linux期末複習

 

 

 

1.判斷用戶輸入的數是否爲迴文數linux

 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.計算用戶輸入的一個數的階乘shell

 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.判斷用戶輸入的數是否爲素數bash

 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項和ui

 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"
相關文章
相關標籤/搜索