Shell scripts: Exercises

Source: Internet
Author: User

Topic 1:1-100 summation

Non-recursive version:

#!/bin/bashsum=0while [$val-le]dolet sum+=vallet val++doneecho $sumsum =0for i in {1..100}doif [$i = = 1]; Thenbar=${bar}${i}elsebar=${bar}+${i}filet Sum+=idoneecho $bar = $sumsum =0for ((i=1; i<=100; i++)) Dolet sum+= Idoneecho $sum

Operation Result:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/82/F2/wKiom1dmk0CjN-GMAAAq2gmytyE714.png "title=" QQ picture 20160619204228.png "alt=" Wkiom1dmk0cjn-gmaaaq2gmytye714.png "/>

Recursive version:

#!/bin/bashfunction Add () {local count=$1if [$count-eq 1];thenecho 1return filocal tmp= $countlet--tmplocal ret= ' Add $tm P ' let Ret+=countecho $ret}read cret=$ (add $c) echo "$ret"

Operation Result:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/82/F2/wKiom1dmn4DDGJ_aAAANOYe27X4469.png "title=" QQ picture 20160619213447.png "alt=" Wkiom1dmn4ddgj_aaaanoye27x4469.png "/>


BC Calculation:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/82/F1/wKioL1dmoJ-Bw2hlAAAPH3YRLXE921.png "title=" QQ picture 20160619213923.png "alt=" Wkiol1dmoj-bw2hlaaaph3yrlxe921.png "/>

2 string conversion, before and after number conversion, letter case conversion:

#!/bin/bashfunction Str () {While Read strdostr1= ' echo ${str:0:3} ' str2= ' echo ${str:3:3} | tr ' [A-z] ' [A-z] ' str3= ' echo ${ Str:6:3} ' printf '%s%s%s\n "" $str 3 "" $str 2 "" $str 1 "done<file}str

Operation Result:
Before:
753asd456

789dfg123

789fdh456

After the change:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/82/F2/wKiom1dmoSjyG4i8AAAUFw6k_E0745.png "title=" QQ picture 20160619163702.png "alt=" Wkiom1dmosjyg4i8aaaufw6k_e0745.png "/>


Find the maximum minimum value of a number for a row in a file:

#!/bin/bashread Nameavg=0max=0min=0cur=0sum=0count=0#cat $name | While read Linewhile read Linedo letcur= $lineif [$count-eq 0];then max= $line min= $linefiif [$cur-gt $max];then max=$ Cur fi if [$cur-le $min];then min= $curfilet count++let sum+=curdone< $nameecho "MAX: $max" echo "min: $min" echo " SUM: $sum COUNT: $count "echo" Average: $ ((sum/count)) "

Operation Result:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/82/F1/wKioL1dmobvidc8rAAAbLrOwekg946.png "title=" QQ picture 20160619162411.png "alt=" Wkiol1dmobvidc8raaablrowekg946.png "/>


Progress bar:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/82/E2/wKioL1djq4DjUi6rAABuyf0WTE8481.png "alt=" Wkiol1djq4djui6raabuyf0wte8481.png (566x365) "/>

Fibonacci Sequence:

#!/bin/basharry[0]=1arry[1]=2temp=0sum=0for i in {0..99}do if [$i-gt 0];thenletarry[$i +1]=arry[$i]+arry[$i-1] Fidoneecho ${arry[99]}

Operation Result:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/82/F2/wKiom1dmoqrj-0u3AAAM-HbKqwU568.png "title=" QQ picture 20160619163030.png "alt=" Wkiom1dmoqrj-0u3aaam-hbkqwu568.png "/>

This article is from the "egg-left" blog, please be sure to keep this source http://memory73.blog.51cto.com/10530560/1790875

Shell scripts: Exercises

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.