Shell Script value: operator

Source: Internet
Author: User
Tags arithmetic arithmetic operators

Arithmetic 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.

Example: Implementing two number additions

#!/bin/bashval= 'expr22'echo'total value: ${ val}"

There are spaces between the expression and the operator , and each expression and operator can be seen as a parameter passed to expr

The following is a list of arithmetic operators, where multiplication sign (*) must have a backslash (\) in order 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 is a list of relational operators

An example of a relational operator is given below

#!/bin/sha=tenb=if [$a-eq $b]   Then Echo " $a-eq $b: A is equal to B " Else   Echo " $a-eq $b: A is not equal to B "

NOTE: Conditional expressions are placed between square brackets and have spaces

Boolean operator

The following is an example of a Boolean operator. Where -O is OR operation,-A is associated with the operation

#!/bin/SHa=Tenb= -if[$a! =$b] Then   Echo "$a! = $b: equal to B"fiif[$a-lt --A $b-GT the ] Then   Echo "$a-lt 100-a $b-gt 15:returns true"fiif[$a-lt --O $b-gt - ] Then   Echo "$a-lt 100-o $b-gt 100:returns true"fi

String operators

The following is a list of string operators

The following is an example of a string operator

#!/bin/SHa="ABC"b="EFG"if[$a =$b] Then   Echo "$a = $b: A is equal to B"Else   Echo "$a = $b: A is not equal to B"if[ -Z $a] Then   Echo "- Z $a: string length is zero"Else   Echo "- Z $a: string length is not zero"fiif[ -N $a] Then   Echo "- N $a: string length is not zero"Else   Echo "- N $a: string length is zero"fiif[$a] Then   Echo "$a: String is not empty"Else   Echo "$a: string is empty"fi

File Test Operators

The following is a list of file test operators

The following is an example of a file test operator

#!/bin/bashfile="/home/runnyu/demo.c"if [-R $file ] then   echo"File has Read access"else   echo"File does not has read acces"fi

Shell Script value: operator

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.