当前位置:网站首页>Shell basic operator -- arithmetic operator
Shell basic operator -- arithmetic operator
2022-06-24 08:37:00 【Chen Bucheng I】
Shell Like any programming language , Support for multiple operators , Include :
- Arithmetic operator
- Relational operator
- Boolean operator
- String operators
- File test operators
Native bash Simple mathematical operations are not supported , But you can do it with other commands , for example awk and expr,expr The most commonly used .expr Is an expression calculation tool , It can be used to evaluate expressions . Add two numbers ( Note the use of back quotes ` Not single quotes ') example
#!/bin/bashval=`expr 2 + 2`echo " The sum of the two is : $val"
Execute the script , The output is as follows :
The sum of the two is:4
Two points attention : Space between expression and operator , for example 2+2 It is not right , Must be written as 2 + 2, This is different from most programming languages we are familiar with . The complete expression is to be ` ` contain , Note that this character is not a common single quotation mark , stay Esc Key bottom .
Arithmetic operator
The following table lists the common arithmetic operators , Assumed variable a by 10, Variable b by 20:
Operator | explain | give an example |
|---|---|---|
+ | Add | `expr $a + $b` The result is 30. |
- | Subtraction | `expr $a - $b` The result is -10. |
* | Multiplication | `expr $a * $b` The result is 200. |
/ | division | `expr $b / $a` The result is 2. |
% | Remainder | `expr $b % $a` The result is 0. |
= | assignment | a=$b Will put the variable b The value is assigned to a. |
== | equal . Used to compare two numbers , Same returns true. | [ $a == $b ] return false. |
!= | It's not equal . Used to compare two numbers , If not, return to true. | [ $a != $b ] return true. |
Be careful : Conditional expressions should be placed between square brackets , And there should be spaces , for example : [$a$b] It's wrong. , Must be written as [ $a$b ]. Examples of arithmetic operators are as follows :
#!/bin/basha=10b=20val=`expr $a + $b`echo "a + b : $val"val=`expr $a - $b`echo "a - b : $val"val=`expr $a \* $b`echo "a * b : $val"val=`expr $b / $a`echo "b / a : $val"val=`expr $b % $a`echo "b % a : $val"if[ $a == $b ]thenecho "a be equal to b"fiif[ $a != $b ]thenecho "a It's not equal to b"fi
Execute the script , The output is as follows :
a + b :30a - b :-10a * b :200b / a :2b % a :0a It's not equal to b
Be careful :
Multiplication sign (*) There must be a backslash in the front (\) To achieve multiplication ;
边栏推荐
- "Adobe international certification" Photoshop software, about drawing tutorial?
- The article takes you to understand the security of Windows operating system and protect your computer from infringement
- 貸款五級分類
- pyQt 中 QMenu 响应
- ZUCC_ Principles of compiling language and compilation_ Experiment 08 parsing LR parsing
- Small sample fault diagnosis - attention mechanism code - Implementation of bigru code parsing
- New technology practice, encapsulating the permission application library step by step with the activity results API
- Take my brother to do the project. It's cold
- Qmenu response in pyqt
- Three categories of financial assets under the new standards: AMC, fvoci and FVTPL
猜你喜欢

ZUCC_ Principles of compiling language and compilation_ Experiment 05 regular expression, finite automata, lexical analysis

问题3 — messageBox弹框,修改默认背景色

ZUCC_编译语言原理与编译_实验08 语法分析 LR 分析

一文带你了解Windows操作系统安全,保护自己的电脑不受侵害

Markdown to realize text link jump

Opencv实现图像的基本变换

ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar

Live broadcast review | detailed explanation of koordinator architecture of cloud native hybrid system (complete ppt attached)

OpenCV to realize the basic transformation of image

How to improve the customer retention rate in the operation of independent stations? Customer segmentation is very important!
随机推荐
OpenCV get(propId) 常用的值
(PKCS1) RSA 公私钥 pem 文件解析
Question bank and simulation examination for operation certificate of refrigeration and air conditioning equipment in 2022
ZUCC_ Principles of compiling language and compilation_ Experiment 05 regular expression, finite automata, lexical analysis
2022 tea artist (intermediate) work license question bank and online simulation examination
Markdown 实现文内链接跳转
Vscode install the remote -wsl plug-in to connect to the local WSL
WPS的JS宏实现图片正文在同一段落的分离方法
Qt导出PDF文件的两种方法
相机投影矩阵计算
PAT 1157:校庆
问题3 — messageBox弹框,修改默认背景色
Tool functions – get all files in the project folder
2021-06-25: a batch of strings consisting only of lowercase letters (a~z) are put
成为IEEE学生会员
Matlab求解线性方程组Ax=b
487. number of maximum consecutive 1 II ●●
win11在cmder中使用vim查看内容的时候空白
QT writing security video monitoring system 36 onvif continuous movement
Export MySQL database to xxx SQL, set xxx The SQL file is imported into MySQL on the server. Project deployment.