Shell Scripting Exercises

Source: Internet
Author: User

    1. Calculate the sum of the 1~100 of the word:

Require the accumulation of numbers, you can use the loop to complete, the following code:

#!/bin/bashsum=0str= "" #for i in {1..100}for ((i=1; i<=100; ++i)) does str+= "${i}+" Let Sum+=idoneecho "${str%+}=${ Sum} "

To run the script:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/83/0A/wKioL1dpLmqws9nsAAAbQ0g7cUQ310.png "title=" Shell1.png "alt=" Wkiol1dplmqws9nsaaabq0g7cuq310.png "/>


Another way to use recursion is to:

#!/bin/bashread num sum=0str= "" function Sum_fun () {num1=$1 if [${num1}-le 1];then//returns echo directly when the value is less than or equal to 1 $n Um1 return fi let--num1 tmp=$ (sum_fun $num 1)//recursive call let sum=$1+ $tmp echo ${sum}}sum=$ (Sum_fun $num ) echo "${sum}"

When you use the $ () command substitution, the commands inside the parentheses are executed, so a new process is created to execute the command, which means that the variables are valid within a process, so you need to be careful when you use them.

To run the program:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/83/0B/wKiom1dpL2TDsdTRAAAOAz2doQg510.png "title=" Shell2.png "alt=" Wkiom1dpl2tdsdtraaaoaz2doqg510.png "/>


2. Progress bar

To write a simulation progress bar using a shell script:

#!/bin/bashstr= "" Arr= ('-"\" | " '/')//define array to simulate cursor rotation for ((i=1; i<=100; ++i)) does let tmp=${i}%4 str+= "#" printf "[%-100s][${i}%%] ${arr[$tmp]} \ r" "${str}"//Only line will not sleep 0.1 doneprintf "\ n"


To run the program:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/83/0A/wKioL1dpMCOgKMMxAAAJrNtGHEA737.png "style=" float: none; "title=" Shell3.png "alt=" Wkiol1dpmcogkmmxaaajrntghea737.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/83/0B/wKiom1dpMCTx30_UAAAPspzJvRw539.png "style=" float: none; "title=" Shell4.png "alt=" Wkiom1dpmctx30_uaaapspzjvrw539.png "/>


3. Find the maximum, minimum, and average value of the data in a file with a precision of 2

The data value in the file is one row of data;

#!/bin/bashsum=0count=0while read line//++count//compute data count by rows [$count-eq 1] && min= $line && max= $line//Initialize the maximum minimum value so that it is equal to the first data let sum+= $line//calculated value and [$line-gt $max] && max= $line//If the data is greater than the maximum value, update max [$ Line-lt $min] && min= $line//If the data is less than the minimum value, update the minimum done<file//output redirect echo "Max: ${max}" echo "min: ${min}" printf "argv : "Echo" Base=10;scale=2;${sum}/${count} "| Bc

To run the program:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/83/0B/wKiom1dpMXfxvGetAAAezW2bZt4973.png "title=" Shell5.png "alt=" Wkiom1dpmxfxvgetaaaezw2bzt4973.png "/>


4. To find the nth Fibonacci number

To find the nth Fibonacci number, you need to know the number N-1 and the N-2 Fibonacci numbers.

#!/bin/bash//uses three variables in turn, the highest efficiency read numval1=0//defines the first two digits val2=1tmp= $val 1for ((i=0; i < $num; ++i)) do let tmp= $val 1+ $val 2 val1= $val 2 val2= $tmpdoneecho "$num: $tmp"


To run the program:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/83/0B/wKiom1dpMuuQ8DLhAAAwOY0eKJs526.png "title=" Shell6.png "alt=" Wkiom1dpmuuq8dlhaaawoy0ekjs526.png "/>


Using recursion is still possible, but less efficient:

#!/bin/bashread num function fib () {num1=$1 if [$1-lt 3];then echo $num 1 return fi let--nu M1 val1=$ (fib $num 1) Let--num1 val2=$ (fib $num 1) Let ret= $val 1+ $val 2 echo $ret}ret=$ (fib $num) echo "$num : $ret "


To run the program:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/83/0A/wKioL1dpM7fRCGctAAAmGG6j8s0301.png "title=" Shell7.png "alt=" Wkiol1dpm7frcgctaaamgg6j8s0301.png "/>


5. Convert the string of each line in a file, for example, convert "123abc456" to "456abc123"

While read linedo//uses the Cut tool for string interception num1=$ (Echo ${line} | cut-c-3)//intercepts all characters from the left 3rd character to the left num2=$ (Echo ${line} | cut-c 7 -)//intercept all characters from the 7th character of the left to the right str=$ (Echo ${line} | cut-c 4-6)//Intercept the 4th character to the 6th character between the left and the next characters//another string intercept way # num1=${line:0:3}# num2=${ line:6:3}# Str=${line:3:3} eval echo "${num2}${str}${num1} | tr [A-z] [a-z] "//Convert with TR tool Done<file


To run the program:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/83/0B/wKiom1dpNNDi2ZZEAAAqHISykgE818.png "title=" Shell8.png "alt=" Wkiom1dpnndi2zzeaaaqhisykge818.png "/>



Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1791499

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.