10.Shell operator

Source: Internet
Author: User
Tags arithmetic arithmetic operators

Bash supports many operators, including arithmetic operators, relational operators, Boolean operators, string operators, and 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:

#!/bin/bash

caoliu= ' Expr 2 + 2 '

Echo $caoliu


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.

Arithmetic operators

Let's take a look at an example that uses arithmetic operators:

#!/bin/sha=10b=20val= ' expr $a + $b ' echo ' A + B: $val ' val= ' expr $a-$b ' echo ' A-B: $val ' val= ' expr $a \* $b ' echo ' A * : $val "val=" expr $b/$a ' echo ' b/a: $val "val=" expr $b% $a ' echo ' B% A: $val "if [$a = = $b]then echo" a equals B "Fiif [$ A! = $b]then echo "A not equal to B" fi

Results:

A + b:30

A-B: 10

A * b:200

B/A: 2

B% a:0

A is not equal to B

Attention:

    • Multiplication sign (*) must be added in front of the backslash (\) to achieve the multiplication operation;

    • If...then...fi is a conditional statement that will be explained later.


This article is from the "8159085" blog, please be sure to keep this source http://8169085.blog.51cto.com/8159085/1793549

10.Shell 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.