Conditional testing and looping statements in the shell

Source: Internet
Author: User

1. Condition testing: Test or [

If the test result is true, the exit status of the command is 0, and if the test result is false, the exit status of the command is 0

Operation Result:

Test commands with and, or, not
[ ! Expr]: Expr can be any of the test conditions in the table above. Represents a logical inverse
[Expr1-a EXPR2]: EXPR1 and EXPR2 can be any of the test conditions in the table above,-a represents the logic and
[Expr1-o EXPR2]: EXPR1 and EXPR2 can be any of the test conditions in the table above,-o represents a logical OR

2. If/then/elif/else/fi

The shell uses a few commands such as if, then, elif, else, fi to implement branch control

Operation Result:

The shell also offers && | | Syntax,&& equivalent to "If...then ...", while | | Equivalent to "if Not...then ...".

-A and-O are only used to connect two test conditions in a test expression

3, Case/esac

Shell script case can match strings and wildcard, each matching branch can have several commands, at the end must be;; End, executes the row to find the first matching branch and executes the line corresponding command, and then jumps directly to Esac

Operation Result:

Looping statements

4, For/do/done

Here's an example:

Operation Result:

5, While/do/done

Here's an example:

Operation Result:

6. Positional parameters and special variables
There are many special variables that are automatically assigned by the shell, and the following are common positional and special variables
$ argv[0 equivalent to the C language main function]
$ ... C language words main function of argv[1], argv[2] ...
These are called positional parameters (positional Parameter)
$# equivalent to the argc-1 of the C language main function
[Email protected] Indicates the parameter list "$" "$" ..., for example, can be used in the for loop in the back.
$? Exit Status of the previous command

Conditional testing and looping statements in the shell

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.