Shell Script Learning Finishing (ii)

Source: Internet
Author: User
Tags case statement

Condition Test--test or [

The command "test" or "[" can test whether a condition is true, if the test result is real, then the exit code of the command is 0, if the test result is false, then the exit code of the command is 1.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/85/AA/wKioL1er5kiTqnyvAAA_7_zW4co722.png "style=" float: none; "title=" Qq20160809224645.png "alt=" Wkiol1er5kitqnyvaaa_7_zw4co722.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/85/AB/wKiom1er5kmwEQrbAAAPPhWalnQ395.png "style=" float: none; "title=" Qq20160809224619.png "alt=" Wkiom1er5kmweqrbaaapphwalnq395.png "/>

Examples of common test commands:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/85/AB/wKiom1er5mSTb73KAABuKWYiRUI701.png "title=" Qq20160811094808.png "alt=" Wkiom1er5mstb73kaabukwyirui701.png "/>

Note: The left bracket [is the name of a command, and the arguments to the command should be separated by a space, for example, $VAR,-GT, 3,] are [four parameters of the command, they must be separated by a space. The command test or [Parameter form is the same, except that the test command does not require] parameters.

Test commands with, or, not

[ ! Expr]: Expr can be any of the test conditions described above! Represents a logical inverse

[Expr1-a EXPR2]: EXPR1 and EXPR2 can be any of the above test conditions,-a representation of logic and

[Expr1-o EXPR2]: EXPR1 and EXPR2 can be any of the above test conditions,-o represents a logical OR

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/85/AA/wKioL1er5oSiqt0YAAA8kMzrczY123.png "title=" Qq20160811095723.png "alt=" Wkiol1er5osiqt0yaaa8kmzrczy123.png "/>

Note: If the $string variable in the previous example is not defined beforehand, the shell is expanded to an empty string, which results in a syntax error for the test condition (expanded to [-D desktop-a = ' abc '), as a good shell programming habit, You should always place variable values in double quotes (expand to [-D desktop-a "" = ' abc ']).

If/then/elif/else/fi

Similar to the C language, branch control is implemented in the shell with the IF, then, Elif, else, and these commands.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/85/AB/wKiom1er5qKSjDMzAAAv9-xN2_0492.png "title=" Qq20160811100621.png "alt=" Wkiom1er5qksjdmzaaav9-xn2_0492.png "/>

Note: If the two commands are written in the same line, they need to be separated, the lines are written, a single command does not need to be written, the number is changed, and then there is a newline, but this command does not finish, the shell will automatically continue, the next line is followed by then as a command processing.

: is a special command called the Empty command, which does nothing, but exit status is always true.

You can also use the && | | To do the above. && equivalent to "If...then ..." while | | Equivalent to "if Not...then ...".

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/85/AA/wKioL1er5sSxQO52AAA4GdS-aPk375.png "title=" Qq20160811101522.png "alt=" Wkiol1er5ssxqo52aaa4gds-apk375.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/85/AB/wKiom1er5vfRqHTFAAAxKEULaCI014.png "title=" Qq20160811101647.png "alt=" Wkiom1er5vfrqhtfaaaxkeulaci014.png "/>

Case/esac

The case command is analogous to the C-switch/case statement, and ESAC represents the end of the case statement block. The case of a C language can only match integer or character constant expressions, whereas shell script case matches strings and wildcard, each matching branch can have several commands, which must be at the end; At the end, when execution finds the first matching branch and executes the corresponding command, and then jumps directly to ESAC, you do not need to jump out of the same language as C.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/AA/wKioL1er5xfDbZtwAAA6-Rn_B8w508.png "title=" Qq20160811102701.png "alt=" Wkiol1er5xfdbztwaaa6-rn_b8w508.png "/>

For/do/done

The For loop structure of a shell script is very different from the C language, and it resembles a foreach loop for some programming languages.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/85/AA/wKioL1er5zCRSPBYAABx6fbKtAk577.png "title=" Qq20160811103305.png "alt=" Wkiol1er5zcrspbyaabx6fbktak577.png "/>

C-Type Writing

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/85/AB/wKiom1er50aAHZgjAAB107abekA314.png "title=" Qq20160811103611.png "alt=" Wkiom1er50aahzgjaab107abeka314.png "/>

While/do/done

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/85/AA/wKioL1er51fjU2fWAAAy7UzqZeA848.png "title=" Qq20160811104020.png "alt=" Wkiol1er51fju2fwaaay7uzqzea848.png "/>

This article is from the "July boreas" blog, please be sure to keep this source http://luminous.blog.51cto.com/10797288/1836790

Shell Script Learning Finishing (ii)

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.