Bash operations, tests, if statements, and for loop statements

Source: Internet
Author: User
Tags arithmetic operators

I. logical operations

Boolean: true or false

And operation:

True & True = true

True & false = false

False & True = false

False & false = false

Or operation:

True | true = true

True | false = true

False | true = true

False | false = false

Non-calculation:

False = false

Non-false = true

Ii. Status Return Value

Echo $?

Success: 0 is returned.

Failed: 1-255 is returned. Failure is not equal to false.

Custom: exit [N]

Iii. Arithmetic Operations

1. Define Integer Variables

Let var_name = integer_value

Declare-I var_name = integer_value

If no integer variable is defined, the numeric type will be converted by Bash to participate in arithmetic operation.

2. Calculation Method

Let var_name = arithmatic_expression

Var_name = $ [arithmatic_expression]

Var_name = $ (expression ))

Var_name = $ (expr expression)

3. Arithmetic Operators

+,-, *,/, %, **, + =,-+, * =,/=, % =, ++

Iv. Comparison Test

1. Integer comparison test

-GT: greater

-Lt: less

-Ge: greater than or equal

-Le: less than or equal

-EQ: equal

-Ne: not equal

2. String comparison test. compare strings based on the size of the ascll code.

Binary operators:

>:

<:

>=:

<=:

=:

! =:

= ~ : Pattern matching, ["$ Var" = ~ Pattern]

Single Object OPERATOR:

-N string: whether it is null. If it is null, it is true. If it is null, it is false.

-Z string: whether it is null. If it is null, it is true. If it is not null, it is false.

3. file comparison test

-A/-E file: if the file exists, it is true. Otherwise, it is false.

-F file: if a common file exists, it is true. Otherwise, it is false.

-D file: the directory file that exists and is true; otherwise, the file is false.

-S file: an existing and non-empty file is true. Otherwise, the file is false.

-R file: If a readable file exists, it is true. Otherwise, it is false.

-W file: an existing and writable file is true. Otherwise, the file is false.

-X file: if an executable file exists, it is true. Otherwise, it is false.

-L/-H: indicates that an existing file with a symbolic link is true. Otherwise, the file is false.

-B file: If the block device already exists, it is true. Otherwise, it is false.

-C file: If a character device exists, the device is true. Otherwise, the device is false.

-S file: The file that exists and is a Socket socket is true. Otherwise, the file is false.

File1-nt file2: The mtime of file1 is true if it is newer than file2; otherwise, it is false.

File1-ot file2: The mtime of file1 is true if it is earlier than file2; otherwise, it is false.

V. Test Methods

1. Test expr

2. [expr]

3. [[expr]

Test comparison is usually performed only between the same type

6. For Loop statements

Views a limited list of elements. The statements are separated by commas (,) in the same row.

Syntax:

For var_name in list

Do

Loop body

Done

List: List, which contains one or more elements.

Exit condition: the traversal ends.

VII. If Condition Statement


Single branch if statement:


If test condition; then

Select Branch

Fi


If statement with two branches:


If test condition; then

Select branch 1

Else

Select branch 2

Fi

Multi-branch if statement:


If test condition 1; then

Select branch 1

Elif test condition 2; then

Select branch 2

Elif test condition 2; then

Select Branch 3

...

Elif test condition n; then

Select branch n

Else

Select branch {n + 1}

Fi










This article from the "Xiao Yang" blog, please be sure to keep this source http://princepar.blog.51cto.com/1448665/1653459

Bash operations, tests, if statements, and for loop statements

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.