Ask for 1-100 and
A. Non-recursive
1 #!/bin/bash 2 sum=0 3 str= "" 4 for I in {1..100} 5 does 6 let sum+=i 7 str+= "$i +" 8 done 9 echo ${str%+}= $sum
Output Result:
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M02/83/0C/wKiom1dpUO2jHDiqAAAr7FYSqrc007.png "title=" sum. PNG "alt=" Wkiom1dpuo2jhdiqaaar7fysqrc007.png "/>
B. Recursion
function Fun_sum () () (num=$1)-sum=0 if [${num}-le 1];then echo 1 return num--sum=$1+$ (fun_sum $num)--------Echo ${sum} N sum=$ (fun_sum $n) echo "${sum}" (+)-Main
Output Result:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/83/3D/wKioL1dtTc2QPjiYAAAX070RkV8648.png "title=" 00.PNG "alt=" Wkiol1dttc2qpjiyaaax070rkv8648.png "/>
2. Find the maximum, minimum, and average value of the data in a file with a precision of 2
1 #!/bin/bash 2 count=0 3 4 while read line 5 do 6 let count++ 7 let sum+= $line 8 if [ $ count -eq 1 ];then 9 max= $line 10 min= $line 11 ave=0 12 fi 13 [ $max -lt $line ] && max= $line 14 [ $min -gt $line ] && min= $line 15 done<file 16 echo "max= $max" 17 echo "min= $min" 18 echo "Ave= ' echo "Base=10; scale=2; ${sum}/${count}" &NBSP;|&NBSP;BC ' "
Output Result:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/83/68/wKiom1dyc_mSJo_NAAAi2yh1WYM114.png "title=" capture. PNG "alt=" Wkiom1dyc_msjo_naaai2yh1wym114.png "/>
3.Fib
A. Non-recursive (loop)
&NBSP;&NBSP;1&NBSP;#!/BIN/BASH&NBSP;&NBSP;2&NBSP;FUNCTION&NBSP;FIB () 3 { 4 n=$1 5 a=1 6 b=1 7 if [ ${n} -eq 1 -o ${n} -eq 2 ];then 8 echo 1 9 return 10 fi 11 while [ ${n} -gt 2 ] 12 do 13 let c=a+b 14 a= $b 15 b= $c 16 let n-- 17 done 18 echo ${c} 19 return 20 } 21 function main () 22 { 23 read n 24 fib $n 25 } 26 main
Output Result:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/83/69/wKiom1dyi9aykYnkAAAMe6Z0as4792.png "title=" capture. PNG "alt=" Wkiom1dyi9aykynkaaame6z0as4792.png "/>
B. recursion
function fib () (n=$1) [$n-eq 1-o $n-eq 2];then echo 1 return to fi 30 Let n--val1=$ (fib $n) n--val2=$ (fib $n) ret= $val 1+ $val 2 #ret =$ (FIB (${n}-1 )) +$ (FIB (${n}-2))//Why is this not working? Echo $ret Notoginseng return 38}
Output Result:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/83/69/wKioL1dzGr7hllcBAAAXQbDYzE4745.png "title=" capture. PNG "alt=" Wkiol1dzgr7hllcbaaaxqbdyze4745.png "/>
4. Convert the string of each line in a file, for example, convert "123abc456" to "456abc123"
1 #!/bin/bash 2 while read line 3 do 4 # num1=$ (echo $line | cut -c -3) #方法一 5 # num2=$ (echo $line | cut -c 4-6) 6 # num3=$ (echo $line | cut -c 7-) 7 num1=${line:0:3} #方法一 8 num2=${line:3:3} 9 num3=${line:6:3} 10 eval echo "${num3}${num2}${num1} | tr [a-z] [a-z]" 11 done <file0
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/83/6A/wKiom1dzIO2xqBfvAAANccLmmek987.png "title=" capture. PNG "alt=" Wkiom1dzio2xqbfvaaancclmmek987.png "/>
This article is from the "sunshine225" blog, make sure to keep this source http://10707460.blog.51cto.com/10697460/1793853
Shell Small Script