Shell script Programming Exercises

Source: Internet
Author: User

Create loops in shell scripts to append echo content redirection to a file

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/82/E7/wKioL1dj6XHwzJ-tAAAoItNjmwc457.png-wh_500x0-wm_3 -wmp_4-s_2295541797.png "title=" qq picture 20160617201208.png "alt=" Wkiol1dj6xhwzj-taaaoitnjmwc457.png-wh_50 "/>

The file is empty before running the script with the contents

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/82/E8/wKiom1dj6ZvRZ6DNAAA5xTG3PHo743.png-wh_500x0-wm_3 -wmp_4-s_1456868078.png "title=" qq picture 20160617201138.png "alt=" Wkiom1dj6zvrz6dnaaa5xtg3pho743.png-wh_50 "/>


Creating an array in a shell script

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/82/E7/wKioL1dj3f7wjqQVAACMijqMQfE180.png-wh_500x0-wm_3 -wmp_4-s_2752854048.png "title=" haha. png "alt=" wkiol1dj3f7wjqqvaacmijqmqfe180.png-wh_50 "/>

Results:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/82/E8/wKiom1dj3Pej7K_0AAAhgf3RaOE209.png-wh_500x0-wm_3 -wmp_4-s_1924983689.png "title=" session. png "alt=" wkiom1dj3pej7k_0aaahgf3raoe209.png-wh_50 "/>

Use three loops to iterate through an array and initialize it directly using parentheses to set content.

1.${#arr [@ or *]} indicates the number of elements in the array

2. You can use ${arr[when you take each element of an array]}, just as you access array elements through array subscripts in C

3. You can use ${arr[@]} or use ${arr[*]} to display all elements in the array

4. arrays can accept any type of data, or directly to a certain location of the array directly assigned to the operation, or the non-contiguous array initialization, where the contents of the non-contiguous arrays are empty


1. Use shell scripts to write simple summation operations

The process of displaying data accumulation 1+2+3+4+...+n

Method 1. Use the printf function to print content, which differs from echo in that Echo wraps the line, and that the function does not wrap

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/82/E8/wKiom1dj4Mrjbp7lAABZC7ui1cc815.png-wh_500x0-wm_3 -wmp_4-s_3903546855.png "title=" 1.png "alt=" Wkiom1dj4mrjbp7laabzc7ui1cc815.png-wh_50 "/>


Method 2. Define a string that is appended to the back, minus the + sign when appended to the last number

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/82/E8/wKiom1dj51rQ8A1mAABsX6vcd8Y201.png-wh_500x0-wm_3 -wmp_4-s_2697129865.png "title=" 5.png "alt=" Wkiom1dj51rq8a1maabsx6vcd8y201.png-wh_50 "/>

Both methods run the same result

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/82/E8/wKiom1dj547iwb-UAAA6pSaxxDw521.png-wh_500x0-wm_3 -wmp_4-s_4096657869.png "title=" qq picture 20160617200527.png "alt=" Wkiom1dj547iwb-uaaa6psaxxdw521.png-wh_50 "/>

Method 3: Summing with recursion

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/82/F1/wKiom1dmTOTx_ZdmAABu0amnR9k809.png-wh_500x0-wm_3 -wmp_4-s_3288882582.png "title=" 2.png "alt=" Wkiom1dmtotx_zdmaabu0amnr9k809.png-wh_50 "/>

Results:

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/82/F0/wKioL1dmTWWSppAdAAAhcRioQYM301.png-wh_500x0-wm_3 -wmp_4-s_1568475009.png "title=" 1.png "alt=" Wkiol1dmtwwsppadaaahcrioqym301.png-wh_50 "/>

The Linux system has a BC calculator, which can be calculated

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/82/F0/wKioL1dmThby7ZdvAAAf5cHhO48730.png-wh_500x0-wm_3 -wmp_4-s_1847179956.png "title=" 3.png "alt=" Wkiol1dmthby7zdvaaaf5chho48730.png-wh_50 "/>


2. A simple progress bar

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/82/F0/wKioL1dmT8yAoP7jAACPIOvGJyo837.png-wh_500x0-wm_3 -wmp_4-s_3163852889.png "title=" 4.png "alt=" Wkiol1dmt8yaop7jaacpiovgjyo837.png-wh_50 "/>

Results:

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/82/F0/wKioL1dmT-qTecGgAAAWJb8c9yM709.png-wh_500x0-wm_3 -wmp_4-s_1825616877.png "title=" 5.png "alt=" Wkiol1dmt-qtecggaaawjb8c9ym709.png-wh_50 "/>


3. Case and sequence conversion of strings

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/82/F0/wKioL1dmWiPDUDW_AABTil25InE826.png-wh_500x0-wm_3 -wmp_4-s_245289308.png "title=" qq picture 20160619163842.png "alt=" Wkiol1dmwipdudw_aabtil25ine826.png-wh_50 "/>

Results:

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/82/F1/wKiom1dmWjeCrApgAAAcYrLLZ9k804.png-wh_500x0-wm_3 -wmp_4-s_223790326.png "title=" result. png "alt=" wkiom1dmwjecrapgaaacyrllz9k804.png-wh_50 "/>

Using the cut tool to intercept a specified range of strings in a single character read, the TR tool can convert the case of a character and redirect the hit to the marked content to another file


4. Find the maximum, minimum, and average of three numbers

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/82/F1/wKiom1dmYdrAEIJ3AAC3MyuyAuU631.png-wh_500x0-wm_3 -wmp_4-s_2808172417.png "title=" aha. png "alt=" wkiom1dmydraeij3aac3myuyauu631.png-wh_50 "/>

Results:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/82/F0/wKioL1dmYe6i70BkAABAEmE3Q60652.png-wh_500x0-wm_3 -wmp_4-s_1968891457.png "title=" fruit. png "alt=" wkiol1dmye6i70bkaabaeme3q60652.png-wh_50 "/>


5. Fibonacci Sequence

Method 1. Use three numbers for conduction

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/82/F1/wKiom1dmZS6R0o4mAABbsYs32q0779.png-wh_500x0-wm_3 -wmp_4-s_29727670.png "title=" non-2.png "alt=" Wkiom1dmzs6r0o4maabbsys32q0779.png-wh_50 "/>

Method 2. Borrowing Array evaluation

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/82/F0/wKioL1dmZUKRG_KxAABhOgVLvrg545.png-wh_500x0-wm_3 -wmp_4-s_447722614.png "title=" non-1.png "alt=" Wkiol1dmzukrg_kxaabhogvlvrg545.png-wh_50 "/>

Method 3. Using recursion

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/82/F0/wKioL1dmbZ3htUo-AACamSSYa-o627.png-wh_500x0-wm_3 -wmp_4-s_1449194192.png "title=" qq picture 20160619175857.png "alt=" Wkiol1dmbz3htuo-aacamssya-o627.png-wh_50 "/>

Results:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/82/F0/wKioL1dmZ2bgetVXAAAbIFmXRNs587.png-wh_500x0-wm_3 -wmp_4-s_3087560252.png "title=" qq picture 20160619173519.png "alt=" Wkiol1dmz2bgetvxaaabifmxrns587.png-wh_50 "/>

Shell script Programming 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.