Shell scripts from getting started to complex five (basic operators)

Source: Internet
Author: User
Tags arithmetic arithmetic operators bit set logical operators

The shell supports a variety of operators, including:

Arithmetic operators
Relational operators
Boolean operator
String operators
File Test Operators


Native bash does not support simple math operations, but can be implemented with other commands, such as awk and expr,expr, which are most commonly used.

Expr is an expression evaluation tool that uses it to perform evaluation operations on expressions.

For example, two numbers are added (note the use of anti-quotes ' instead of single quotes '):


# VI test.sh


#!/bin/bash

sum= ' expr 1 + 3 '

echo "Sum of two numbers: $sum"


Output:

# sh test.sh

Sum of two numbers: 4


Attention:

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.


Arithmetic operators

The following table lists the commonly used arithmetic operators, assuming that variable A is 10 and variable B is 20:

Operator Description Example
+ Addition ' Expr $a + $b ' result is 30
- Subtraction ' Expr $a-$b ' result is-10
* Multiplication ' Expr $a \* $b ' result is 200
/ Division ' Expr $b/$a ' result is 2
% Take surplus ' Expr $b% $a ' result is 0
= Assign value A= $b will assign the value of variable B to a
== Equal. Used to compare two numbers, the same returns true [$a = = $b] returns false
!= Equal. Used to compare two numbers, not the same returns true [$a! = $b] Returns True

 

Attention:

Conditional expressions are placed between square brackets and have spaces, for example: [$a = = $b] is wrong and must be written as [$a = = $b].

Multiplication sign (*) must have a backslash (\) in front of it to achieve multiplication.


Relational operators

Relational operators only support numbers, and strings are not supported unless the value of the string is a number.


The following table lists the commonly used relational operators, assuming that variable A is 10 and variable B is 20:

Operator Description Example
-eq Detects whether two numbers are equal and returns true for equality [$a-eq $b] returns false
-ne Detects whether two numbers are not equal and returns True [$a-ne $b] returns True
-gt Detects if the number on the left is greater than the right and, if it is, returns true [$a-gt $b] returns false
-lt Detects if the left number is less than the right, and returns true if it is [$a-lt $b] returns True
-ge Detects if the left number is greater than or equal to the right, and returns true if it is [$a-ge $b] returns false
-le Detects if the left number is less than or equal to the right, and returns true if it is [$a-le $b] returns True


Boolean operator

The following table lists the commonly used Boolean operators, assuming that variable A is 10 and variable B is 20:

Operator Description Example
! Non-operation, the expression is true returns False, otherwise true. [! false] Returns True
-O Or operation, there is an expression of true to return true. [$a-lt 20-o $b-GT 100] Returns True
-A With an operation, two expressions are true to return true. [$a-lt 20-a $b-GT 100] returns false


logical operators

The following is a description of the Shell's logical operators, assuming that variable A is 10 and variable B is 20:


Operator Description Example

&& logical AND[[$a-lt && $b-GT 100]] returns false

|| Logical OR[[$a-lt | | $b-GT 100]] returns True


String operators

The following table lists the commonly used string operators, assuming that variable a is "ABC" and Variable B is "EFG":

Operator Description Example
= 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 whether 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. [-N $a] returns true
Str Detects whether the string is empty and does not return true for null. [$a] returns True

File Test Operators

File test operators are used to detect various properties of Unix files.

Attribute detection is described as follows:

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 [-C $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 if so, returns True [-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 well-known pipe, and if so, returns True [-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 is executable and returns true if it is [-X $file] returns True
-S file Check if file is empty (file size is greater than 0), not NULL returns True [-S $file] returns True
-E File Detects whether the file (including the directory) exists and, if so, returns True [-e $file] returns True

Shell scripts from getting started to complex five (basic operators)

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.