# Shell operator relational operators, arithmetic operators, Boolean operators, string operators, and file test operators

Source: Internet
Author: User

There are many shell operators, relational operators, arithmetic operators, Boolean operators, string operators, and file test operators

1, arithmetic operators

Acoustic Bash does not support simple arithmetic operations, and you can use the Expr tool

Two points Note:

• There are spaces between the expression and the operator, such as the 2 + 2, which is different from most of the programming languages we are familiar with.
• The complete expression is to be contained, note that this character is not a common single quote, below the ESC key.

#!/bin/bash
a=10
B=20
Rst= ' Expr 2 + 2 '
Echo ' \${rst}
rst= ' Expr 2-2 '
echo ' 2-2 = ' \${rst}
rst= ' Expr 2 \* 2 '
echo ' 2*2 = ' \${rst}
rst= ' Expr 2/2 '
Echo ' 2/2 = ' \${rst}
rst= ' Expr 3% 2 '
echo ' 3%2 = ' \${rst}

Output:

4
2-2 = 0
2*2 = 4
2/2 = 1
3%2 = 1

Relational operators

Relational operators support only the numeric part support string

-EQ detects whether two numbers are equal equal returns True

-ne detects whether two numbers are equal or not equal returns True

-GT detects if the number on the left is greater than the right and, if it is, returns true

-LT detects if the number on the left is less than the right and, if it is, returns true

-ge detects if the left number is greater than or equal to the right and returns true if yes

-le detects if the left number is less than or equal to the right and returns true if yes

Example

If [\$a-lt \$b]
Then
echo "A=\${a} b=\${b} a<b"
Fi

A=20
b=10
If [\$a-gt \$b]
Then
echo "A=\${a} b=\${b} a>b"
Fi

A=20
B=20
If [\$a-eq \$b]
Then
echo "A=\${a} b=\${b} a==b"
Fi

a=10
B=20
If [\$a-ne \$b]
Then
echo "A!=b"
Fi

If [\$a-ge \$b]
Then
echo "A>=b"

If [\$a-le \$b]
Then
echo "A<=b" relational operator list

Fi

Boolean operator

！ Non-op [! False

-O or operation [\$a-lt-o \$b-GT 100]

-A and operations [\$a-lt-a \$b-GT 100]

String operators

= detects whether two strings are equal and returns true for equality. [\$a = \$b] returns FALSE.

! = detects whether two strings are equal, and returns true if they are not equal. [\$a! = \$b] Returns TRUE.

-Z detects if the string length is 0 and returns true for 0. [-Z \$a] returns false.

-N detects whether the string length is 0 and does not return true for 0. [-Z \$a] returns true.

STR detects if the string is empty and does not return true for null. [\$a] returns TRUE.

File Test Operators

List of file test operators
operator Description Example
-B File Detects if the file is a block device file, and returns True if it is. [-B \$file] returns FALSE.
-C file Detects if the file is a character device file, and returns True if it is. [-B \$file] returns FALSE.
-D File Detects if the file is a directory, and returns True if it is. [-D \$file] returns false.
-F File Detects if the file is a normal file (neither a directory nor a device file), and returns True if it is. [-F \$file] returns TRUE.
-G file Detects if the file has a SGID bit set, and returns True if it is. [-G \$file] returns false.
-K File Detects if the file has a sticky bit set (Sticky bit), and returns True if it is. [-K \$file] returns false.
-P File Detects if the file is a named pipe, and returns True if it is. [-P \$file] returns false.
-U file Detects if the file has a SUID bit set, and returns True if it is. [-U \$file] returns false.
-R File Detects if the file is readable and returns true if it is. [-R \$file] returns TRUE.
-W File Detects if the file is writable and returns true if it is. [-W \$file] returns TRUE.
-X File Detects if the file can be executed and, if so, returns True. [-X \$file] returns TRUE.
-S file Detects whether the file is empty (the file size is greater than 0) and does not return true for null. [-S \$file] returns TRUE.
-E File Detects whether the file (including the directory) exists and, if so, returns True. [-e \$file] returns TRUE.

Shell operator relational operators, arithmetic operators, Boolean operators, string operators, and file test operators

Related Keywords:
Related Article

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.