Shell Scripting Exercises

Source: Internet
Author: User

First, using the shell script to implement the progress bar

#!/bin/bashfunction proc ()   4 {  5     count=0   6     bar= ""   7     lable= ("|"   "/"   "-"   "\")   8     while [  $count  -le  100 ]  9     do 10              let index=count%4 11              printf  "[\033[1m\033[1;34m%-101s\033[0m] [%d%%] [%c]\ R   $bar   $count   ${lable[$index]} " 12              bar=${bar} ' # '  13               (( count++ ))  14              sleep 0.05 15     done 16     #   printf  "\ n"  17 } 18 proc

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/83/3A/wKiom1ds6kujH-8_AABzRFzVjVE557.jpg-wh_500x0-wm_3 -wmp_4-s_3681275662.jpg "title=" progress bar. JPG "alt=" Wkiom1ds6kujh-8_aabzrfzvjve557.jpg-wh_50 "/>

Ii. seeking 1-100 and (non-recursive)

#!         /bin/bash sum=0 str= "" "for I in {1..100}" sum+=i [$i-eq] && {26 Str=${str}${i} str=${str}${i} ' + ' ((sum=sum+i)) done #echo $sum echo "$str =$ Sum

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/83/3A/wKiom1ds6zzRMxg8AADJ8FLadFs398.jpg-wh_500x0-wm_3 -wmp_4-s_986329583.jpg "title=" 1-100 non-recursive summation. JPG "alt=" Wkiom1ds6zzrmxg8aadj8fladfs398.jpg-wh_50 "/>

Summation of three or 1-100 (recursive)

#!         /bin/bashfunction sum () 4 {5 local val=$1 6 Local res 7 if [$val-eq 1];then 8 echo 1 9 Return ten fi one local tem= $val tem--local tem1=0 tem1=$ (sum $tem) = $tem 1+ $val echo $res) sum 100


650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/83/3A/wKiom1ds6_nwSnGSAABRD9v1TbY735.jpg-wh_500x0-wm_3 -wmp_4-s_3492682169.jpg "title=" 1-100 recursion. JPG "alt=" Wkiom1ds6_nwsngsaabrd9v1tby735.jpg-wh_50 "/>

Iv. to find the maximum, minimum, average value of a number in a file

Three number of file files: 100,200,300

 1 #!/bin/bash  2 count=0  3 while read line  4      do  5         let ++ count  6         [  $count  -eq 1 ]  && min= $line  && max= $line   7          let sum+= $line   8          [  $line  -gt  $max  ] && max= $line   9          [  $line  -lt  $min  ] && min= $line  10      done <file 11     echo  "max: ${ Max} " 12     echo " Min: ${min} " 13      printf  "argv: "  14     echo  "base=10;scale=2; 15     ${sum}/${ Count} "&NBSP;|&NBSP;BC



650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/83/3B/wKiom1ds77HQa0WIAABf3XC92sc476.jpg-wh_500x0-wm_3 -wmp_4-s_1268445071.jpg "title=" mean. JPG "alt=" Wkiom1ds77hqa0wiaabf3xc92sc476.jpg-wh_50 "/>



V. Fibonacci sequence (non-recursive)

 54 #!/bin/bash 55  56 function fib ()  57 { 58     read val 59     fib_ arr[1]=1 60     fib_arr[2]=1 61     local  count=0 62     while [  $count  -le  $val  ] 63      do 64         let fib_arr[$ count+2]=fib_arr[$count +1]+fib_arr[$count] 65          ( count++))  66     done 67     echo ${fib_arr[$ Val]} 68 } 69 fib 

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/83/3A/wKioL1ds-AHzMGi6AACeHA28HgU614.jpg-wh_500x0-wm_3 -wmp_4-s_2750262765.jpg "title=" Fibonacci recursion. JPG "alt=" Wkiol1ds-ahzmgi6aaceha28hgu614.jpg-wh_50 "/>


Vi. Fibonacci (Recursive)

#!/bin/bashfunction fib ()  76 { 77     local val=$1 78      local fiv_val1=0 79     local fiv_val2=0  80     local fiv_curr=0 81     if [  $1 -lt 3 ];then 82              echo 1 83              Return 84     fi 85     let val-- 86      fiv_val1=$ ( fib  $val  )  87     let  val-- 88     fiv_val2=$ ( fib  $val  )  89      let fiv_curr= $fiv _val1+ $fiv _val2 90     echo  $FIV _ Curr 91 } 92 ret=$ (fib  $val)  93 echo  "$ret" 


650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/83/3A/wKioL1ds9WXReVzSAACeHA28HgU403.jpg-wh_500x0-wm_3 -wmp_4-s_1359898093.jpg "title=" Fibonacci recursion. JPG "alt=" Wkiol1ds9wxrevzsaaceha28hgu403.jpg-wh_50 "/>


Convert a string in a price: for example: "123abc456", "456abc123"

The file is "123abc456" converted to "456abc123"

#!/bin/bash Read name str1=$ (echo $line | cut-c 1-3) str2=$ (echo $line | cu T-c 4-6 | TR ' [A-z] ' [A-z] ') str3=$ (echo $line | cut-c 7-9) echo ${str3}${str2}${str1} done< $name >${name}_ Bak function Main () {read target sum $target)



650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/83/3B/wKiom1ds9pyyYaHcAABXonR1Qk4884.jpg-wh_500x0-wm_3 -wmp_4-s_2279043478.jpg "title=" flip. JPG "alt=" Wkiom1ds9pyyyahcaabxonr1qk4884.jpg-wh_50 "/>






Shell Scripting 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.