Shell scripts, testing

Source: Internet
Author: User
Tags arithmetic readable shebang

First we need to understand the content and format of the script

    1. The first line must be shebang (interpreter path), shebang must occupy the absolute beginning

    2. In the shell script, except for Shebang, the contents of the line beginning with # are all comment lines, and the interpreter automatically ignores such lines.

    3. In order to be able to highlight the function of the program in the program, appropriate to add blank lines, to separate different source code blocks

    4. There is a certain indentation

Using bash scripting to implement arithmetic operations:

+,-

*、/

%: modulo operation, taking remainder

**|^

Enhanced arithmetic operation symbols

+ =: let B=b+2-->let b+=2

-= Let B=b+2-->let b-=2

*= Let B=b+2-->let b*=2

/= Let B=b+2-->let b/=2

%= Let B=b+2-->let b%=2

Special Enhanced Operations:

Let b=b+1-->let b+=1-->let b++

Let b=b-1-->let b-=1-->let b--

b++: First assign the value of B to another variable, and then self +1

++b: Do your own +1 calculations before assigning the value of B to other variables

The arithmetic method provided by the shell

1.let:let-var= Arithmetic expression

Arithmetic expression is first carried out and the moral result of the operation is saved in the variable var

2.var=$[Arithmetic expression]

In an arithmetic expression, if there is a variable reference, you can use the $ reference or omit the $

3.var=$ ((arithmetic expression))

In an arithmetic expression, if there is a variable reference, you can use the $ reference or omit the $

4.expr ARGU1 ARGU2 ARGU3

ARGU1 and ARGU3 must be numeric, ARGU2 must be an operation symbol

5.echo "Arithmetic expression" | Bc

Note: * In some cases it is necessary to escape

Conditional test Commands

Test EXPRESSION

Shell built-in commands

[EXPRESSION]

external command [

[[EXPRESSION]]

Bash's internal keywords

Note: This type of command generally has no execution result but only execution status return value;

Let's talk about the test expression for the test command.

There are three types of test expressions in the test command:

1. Numerical Test: Binocular operator

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

-ne: The two values tested are unequal, not equal to true, and equal to False

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

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

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

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

2. String test

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 side 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, the left is less than the right, less than true, greater than false

Note that:> and < must be used in [[Exprssion]]

=~: The two strings tested, whether the left string can be matched by the pattern on the right, true if it matches, not a false

Monocular operator

-Z ' string ': Determines whether the specified string is a null string, NULL is true, and not empty is false;

-N ' string ': Determines whether the specified string is a non-empty string, non-null is true, and Null is false;

Note: 1. Usually, the string is quoted, single quotation marks can be, according to the actual situation to choose

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

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

3. File Status Test

Monocular test

The existence of the file test: If the file being tested exists as true, there is no false

-A|-E [-e/backup]

File type test: (Test existence 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 whether they 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 linked files;

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

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

Access rights test for files:

-R file: The document being tested exists and is currently valid user readable, the file exists and the current valid user is readable as true, otherwise false

-W File: The file being tested exists and is currently valid user writable

-X file: whether the file being tested exists and is currently active user class execution

Special permission identification test for files

-u file: The file being tested exists and the SUID permission is set, the file exists and the SUID permission is set to True, otherwise false

-G file: The file being tested exists and Sgid permissions are set

-K file: The files being tested are present and sticky permissions are set

Ownership test of the file:

-O File: Whether the file being tested exists and whether its owner is the current active user,

-G File: Whether the file being tested exists and whether the group is a group of currently active users,

Whether the file contents are empty:

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

Time Stamp test:

-N File: The tested file has been modified since the last time it was modified, not modified to true, modified to False

Binocular test:

File1-ef FILE2: Test whether two files point to the same inode hard link on the same file system

File1-nt FILE2: Two files tested, File1 is newer than FILE2

File1-ot FILE2: Two files tested, File1 is older than FILE2

You can add a logical operation in a test statement

[-o/tmp/test-a-s/tmp/test] with O Yes or! Take counter

Execution status of the command:

Normal output results:

The execution status return value of the command:

0~255

When the shell script runs, once the Exit command is encountered, the current shell will be terminated immediately, and the script will be stopped, that is, the command after exit is not interpreted;


Shell scripts, testing

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.