function and cyclic control of Linux--shell programming

Source: Internet
Author: User

Functions: Code snippets that implement standalone functionality

function is only executed when called

Syntax One:

function f_name{

function body

}

Syntax Two:

F_name () {

function body

}

The return value of the function:

Default function return value: Function execution state return value, default is the state value executed by the last command in the script, no actual use

Custom function return value: return [0-255]

0: Success

Non 0: Failed

 function Example

  

  

Loop control:

Continue: End this cycle in advance and go directly to the next cycle.

For example, the code will loop five times after execution is complete. In the third cycle, the continue is directly out of the third cycle, the fourth cycle of the body.

    

Break: Terminates the loop body directly, i.e. jumps out of the loop.

For example, the code will loop five times after execution is complete. Break was started in the third cycle. It jumps out of the loop body, and the No. 345 cycle is not executed.

    

A special use of the while loop: iterates through the lines of a file, reading each line of the file one at a time, and then assigning it to variable.

While Read Variable;do

Loop body

Done </path/from/somefile

While special usage example:

    

Special usage for the FOR loop:

For (control variable initialization; conditional judgment expression; control variable correction statement);d O

Loop body

Done

For Loop example

    

  

function and cyclic control of Linux--shell programming

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.