Arithmetic operations and conditional test statements in shell scripts

Source: Internet
Author: User
Tags readable

Shell scripting implements arithmetic operations that are:

+,-

*、/

Modulo operation%

exponentiation * * or ^


Special Enhanced arithmetic operations:

++ --


Arithmetic Operation method

1.let command var= arithmetic expression

The arithmetic expression is first performed and the result of the operation is saved to the variable var

2.var=$[arithmetic expression] [] If you reference a variable, you can use $ or you can omit the $

3.var=$ (arithmetic expression) to replace [] with (())

4.expr ARG1 ARG2 ARG3

ARG1 and ARG3 must be numeric, ARG2 must be an operator

5.echo "Arithmetic expression" | Bc


Note: * In some cases it is necessary to escape


Example: Statistics/etc/fstab/etc/passwd/etc/issue The sum of the number of lines beginning with the letter R in three files

echo $[$ (grep-c ' ^r '/etc/passwd) +$ (grep-c ' ^r '/etc/passwd)]


Conditional Test Command:

1.test Expression

Shell built-in commands


2.[Expression]

external command [;


3.[[Expression]]

Bash's internal keywords;

Note that such commands generally do not result in execution, only the return value of the execution state, for logical judgment;



Test command has three types of test expressions

1. Numerical Test: Binocular operator

-eq: The two values being tested are equal, equal to true, and not as false

-ne: Whether the two values tested are unequal, not equal to true, and are false

-GT: Two values tested, whether left is greater than right, greater than true, not greater than false

-LT: Two values tested, left is less than right, less than true, not less than false

-ge: Two values tested, whether left is greater than or equal to right, greater than or equal to true, less than false

-le: Two values tested, whether left is less than or equal to right, less than or equal to true, greater than false


Example: whether the size of the ~/AAA is less than 1000 bytes

[$ (du A.C | awk ' {print $} ')-lt +] && echo true | | echo False


2. String test

Divided into binocular and monocular

Binocular operator:

= =: The two strings tested are the same, the same is true, the difference is false

! =: The two strings tested are not the same, the difference is true, the same is false

: The two tested strings correspond to the binary values in the ASCII code table, the left is greater than the right, is greater than true, is less than the false

<: The two tested strings in the ASCII code table corresponding to the binary value, whether the left is less than the right, less than true, greater than false

Note that:> and < must be used in test statements [[EXPR]]

=~: Tested two strings, left can be matched to the right pattern, can match to true, cannot be false;

Monocular operator:

-Z ' string ': Determines whether the specified string is an empty string, NULL is true, and non-null is False

-N ' string ': Determines whether the specified string is a non-empty string, non-NULL is true, NULL is False


Attention:

1. Usually, the string to be quoted, according to the actual situation to choose single or double quotation marks

2.[[]] and [] in some cases, may have different meanings, to distinguish between using

3. There are spaces at both ends of the expression and at both ends of the operator


3. File Status Test

Monocular:

1,-a | -E: The existence of a file test: If the file being tested exists as true, there is no false

2. File type Test (existence is tested first)

-B File: whether the files being tested exist and are block devices, present and true for block devices, false otherwise

-C File: whether the files being tested exist and are character devices;

-D file: Whether the files being tested exist and are directory files;

-F File: whether the files being tested exist and are normal files;

-h|-l file: Whether the files being tested exist and whether they are symbolic connection files;

-P File: whether the file being tested exists and is a pipe file;

-S file: whether the files being tested exist and are socket files ...


3. File Access Test

-r file: The file being tested is present and the current valid user is readable, the file exists and the current valid user is readable as true, otherwise it is false;

-W File: If the file being tested exists and the current valid user is writable, the file exists and the current valid user can write it as true, otherwise it is false;

-X file: If the file being tested exists and the current valid user can execute it, the file exists and the current valid user can execute it, otherwise it is false;

4. Special permission identification test for files:

-U file: Whether the file being tested exists and whether the SUID permission is set, the file exists and the suid is set to true, otherwise false;

-G File: Whether the file being tested exists and whether the Sgid permission is set, the file exists and the Sgid is set to true, otherwise false;

-K file: The files being tested are present and if sticky permissions are set, the file exists and the sticky is set to true, otherwise false;

5. File ownership test:

-O File: Whether the file being tested exists and whether the owner is the current active user, the file exists and the owner is the current user is true, otherwise false;

-G File: If the file being tested exists and its genus is a group of the currently active user, the file exists and the group is true for the current user, otherwise it is false;

6. Whether the contents of the file are empty

-S file: The files being tested exist and the contents are not empty, the file exists and the content is not empty for true, otherwise false;

7. Time Stamp test:

-N File: The tested file has been modified since the last time it was modified, changed to true, otherwise false



Binocular:

File1-nt FILE2 True If file1 is newer than file2 (according to modification date).

File1-ot FILE2 True If file1 is older than file2.

File1-ef FILE2 True If file1 is a hard link to file2.


You can add a logical operation in a test statement

The first way of expression

For example [-o/tmp/test] && [-s/tmp/test]: Determine if/tmp/test is all and not empty for the current active user

The second Kind

[-o/tmp/test-a-s/tmp/test] here-a means &&

[-o/tmp/test-o-s/tmp/test] here-o means | |

[!-o/tmp/test] with! [-o/tmp/test] The results are the same, but! The meaning is different; you can write.

Execution result of the command

Normal output results


Execution status return value of the command

0-255

0: Success

1, 2,127: System reserved

3-126 128-255: User-defined return value


Exit [*]

Exit the current Shell login


When the shell script runs, once the Exit command is encountered, the current shell process is terminated, and the script's run is stopped, that is, all commands after exit are no longer executed


Arithmetic operations and conditional test statements in shell scripts

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.