First, calculate the value of 1+2+...+100
Use the General method:
#! /bin/bash sum=0 res= "" num=1 for ((; num<=100; num++)) do let sum+=num [ $ num -eq 100 ]&&{ res=${res}$ {num} break } res=${res}${num} ' + ' done echo ${res}=${sum }
The results are as follows:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/83/12/wKiom1dqKrCya1xzAABmDYIlK7A842.png "title=" 1+...+ 100.png "alt=" Wkiom1dqkrcya1xzaabmdyilk7a842.png "/>
Use recursive methods:
1 #! /bin/bash 2 3 read num 4 5 sum=0 6 function add () 7 { 8 num1= $num 9 [ $num -le 1 ]& &{ 10 echo "$num" 11 return 12 } 13 14 let num-- 15 tmp=$ (add $num) 16 let sum= $num 1+ $tmp 17 echo "$sum" 18 } 19 20 add $num
The results are as follows:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/83/11/wKioL1dqK02xuCRZAAA3CiR00Aw232.png "title=" 1+...+ 100 recursive. png "alt=" Wkiol1dqk02xucrzaaa3cir00aw232.png "/>
Second, the fee spectrum Nahan number
(1) The array method is used: (saves each number calculated)
1 #! /bin/bash 2 3 Read Val 4 arr[0]=1 5 arr[1]=1 6 i=0 7 while [$i-le $val] 8 does 9 let Arr[i+2]=arr[i+1]+arr[i] ten let i++ one-done and Echo ${arr[val]}
The results are as follows:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/83/11/wKioL1dqK-mA3pp0AABjZ0Jm63o249.png "title=" fib_ Array. png "alt=" Wkiol1dqk-ma3pp0aabjz0jm63o249.png "/>
(2) Using recursion method: (Low efficiency)
1 #! /bin/bash 2 3 Read num 4 5 function fib () 6 {7 [$num-le 1]&&{8 echo "$num" 9 re Turn--num val1=$ (fib $num)--num val2=$ (fib $num) let val= $val 1+$ Val2 echo "$val", fib num
The results are as follows:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/83/12/wKiom1dqLCiCAhKaAABzYuXY2QQ931.png "title=" fib_ Recursive. png "alt=" Wkiom1dqlcicahkaaabzyuxy2qq931.png "/>
Three, string splicing
Chestnut Code:
1 #! /bin/bash 2 3 While read line 4 do 5 str1=$ (echo $line |cut-c 1-3) 6 str2=$ (echo $line |cut-c 4-6|tr ' [A-z] ' ' [A-z] ') 7 str3=$ (echo $line |cut-c 7-9) 8 echo ${str3}${str2}${str1} 9 Done<file
The results are as follows:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/83/11/wKioL1dqLHfBjytnAABos7Hqmt0812.png "title=" string concatenation. png "alt=" Wkiol1dqlhfbjytnaabos7hqmt0812.png "/>
Iv. simulating a simple progress bar
Chestnut Code:
1 #! /bin/bash 2 3 val=100 4 str= "" 5 arr= (' | ') '/'-' \ \ ') 6 for ((I=1; i<=val; i++)) 7 does 8 str+= "#" 9 let index=i%4 ten printf "[%-100s] [${i}%%] [$ {arr[$index]}] \ r "" ${str} "one sleep 0.5" \ n "
The results are as follows:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/83/12/wKioL1dqLMmzIYz8AABTc0iDwNo277.png "style=" float: none; "title=" Jindutiao1.png "alt=" Wkiol1dqlmmziyz8aabtc0idwno277.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/83/12/wKioL1dqLM_ifON8AABBS2lXiYI158.png "style=" float: none; "title=" Jindutiao2.png "alt=" Wkiol1dqlm_ifon8aabbs2lxiyi158.png "/>
V. Find the maximum minimum value of the number in the file
Chestnut Code:
1 #! /bin/bash 2 3 count=0 4 while read line 5 do 6 let count++ 7 [ $count -eq 1 ]&&{ 8 min= $line 9 max= $line 10 } 11 [ $max -lt $line ]&&{ 12 max= $line 13 } 14 15 [ $ min -gt $line ]&&{ 16 min=$ line 17 } 18 done<file 19 echo min= $min, max= $max
The results are as follows:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/83/12/wKioL1dqLSnzoGYVAABC52XgLoo621.png "title=" Min_ Max.png "alt=" Wkiol1dqlsnzogyvaabc52xgloo621.png "/>
This article is from "GREEN" blog, please make sure to keep this source http://green906.blog.51cto.com/10697569/1791715
shell--Simple Exercises