当前位置:网站首页>Shell operator
Shell operator
2022-06-25 13:59:00 【Resounding through heaven】
1 expr
1.1 Introduce
expr yes evaluate expressions Abbreviation , Translated into “ Evaluation expression ”.Shell expr Is a powerful , And more complex commands , In addition to integer calculation , You can also combine some options to process strings , For example, calculate the length of a string 、 String comparison 、 string matching 、 String extraction, etc , Follow up explanation .
1.2 expr grammar
Computational syntax
expr Arithmetic operator expression
Be careful : Operation expression
Get the calculation result and assign it to the new variable syntax
result=`expr Arithmetic operator expression `
1.3 demo
#!/bin/bash
result=`expr 2 + 2`
echo "result :${result}"
Introduction to arithmetic operators
The following table lists the common arithmetic operators , Assumed variable a by 1, Variable b by 2:
| Operator | explain | give an example |
|---|---|---|
| + | Add | expr $a + $b The result is 3 |
| - | Subtraction | expr $a - $b The result is -1 |
| * | Multiplication | expr $a \* $b The result is 2 |
| / | 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 |
If used in the four operations (), It also needs to be escaped
\( 1 + 1 \)
#!/bin/bash
read -p " Enter two integers :" a b
echo "a=${a} b=${b}"
rs1=`expr ${
a} + ${
b}`
echo "a + b = ${rs1}"
rs2=`expr ${
a} - ${
b}`
echo "a - b = ${rs2}"
rs3=`expr ${
a} \* ${
b}`
echo "a * b = ${rs3}"
rs4=`expr ${
a} / ${
b}`
echo "a / b = ${rs4}"
rs5=`expr ${
a} % ${
b}`
echo "a % b = ${rs5}"

2 Integer comparison operators
2.1 grammar
The following table lists the common comparison operators , Assumed variable a by 1, Variable b by 2:
| Operator | explain | give an example |
|---|---|---|
-eq | equals Check whether two numbers are equal , Equal return 0, Otherwise return to 1. | [ $a -eq $b ] return 1. |
-ne | not equals Check if two numbers are not equal , Unequal return true. | [ $a -ne $b ] return 0. |
-gt | greater than Check whether the number on the left is greater than that on the right , Is to return 0, otherwise 1 | [ $a -gt $b ] return 1. |
-lt | lower than Check if the number on the left is less than the number on the right , Is to return 0, otherwise 1 | [ $a -lt $b ] return 0. |
-ge | greater equals Check whether the number on the left is equal to or greater than the number on the right , Is to return 0, otherwise 1 | [ $a -ge $b ] return 1. |
-le | lower equals Check whether the number on the left is less than or equal to the number on the right , Is to return 0, otherwise 1 | [ $a -le $b ] return 0. |
< | Check if the number on the left is less than the number on the right , Is to return 0, otherwise 1 | (($a<$b)) return 0 |
<= | Check whether the number on the left is less than or equal to the number on the right , Is to return 0, otherwise 1 | (($a<=$b)) return 0 |
> | Check whether the number on the left is greater than that on the right , Is to return 0, otherwise 1 | (($a>$b)) return 1 |
>= | Check whether the number on the left is equal to or greater than the number on the right , Is to return 0, otherwise 1 | (($a>=$b)) return 1 |
== | Check whether the number on the left is equal to the number on the right , Is to return 0, otherwise 1 | (($a==$b)) return 1 |
!= | Check whether the number on the left is not equal to the number on the right , Is to return 0, otherwise 1 | (($a!=$b)) return 0 |
Be careful :
Integer comparison operators only support integers , Decimals and strings are not supported ( Follow up explanation of string comparison ), Unless the value of the string is an integer number .
Each command has a return value , Later, we will explain the exit status and give more details , return 0 On behalf of success , return 1 For failure
2.2 demo
#!/bin/bash
read -p " Enter two integers :" a b
echo "a: ${a}"
echo "b: ${b}"
if (($a > $b));
then
echo "a Greater than b"
elif (($a == $b)); then
echo "a be equal to b"
else
echo "a Less than b"
fi

3 String comparison operators
You can compare 2 A variable , The type of variable can be a number ( Integers , decimal ) And string
3.1 grammar
The following table lists the common string operators , Assumed variable a by “abc”, Variable b by “efg”:
String comparison can use [[]] and [] 2 Ways of planting
| Operator | explain | give an example |
|---|---|---|
| == or = | equal . Used to compare two strings or numbers , Same returns 0. have access to = | [ $a == $b ] return 1 [ $a = $b ] return 1[[ $a == $b ]] return 1[[ $a = $b ]] return 1 |
| != | It's not equal . Used to compare two strings or numbers , If not, return to 0. | [ $a != $b ] return 0[[ $a != $b ]] return 0 |
| < | Less than , Used to compare two strings or numbers , Less than return 0, Otherwise return to 1 | [ $a \< $b ] return 0[[ $a < $b ]] return 0 |
| > | Greater than , Used to compare two strings or numbers , Greater than return 0, Otherwise return to 1 | [ $a \> $b ] return 1[[ $a > $b ]] return 1 |
| -z | Check if the string length is 0, If the length is 0 Return returns 0, Otherwise return to 1. | [ -z $a ] return false. |
| -n | Check whether the string length is not 0, If the length is not 0 Then return to 0, Otherwise return to 1. | [ -n “$a” ] return true. |
| $ | Check if the string is not empty , Not empty return 0, Empty return 1. | [ $a ] return true. |
String comparison no
<=Can pass[[ "a" < "b" && "a" == "b" ]]>
3.2 demo
#!/bin/bash
read -p " Enter two strings :" a b
echo "a: ${a}"
echo "b: ${b}"
if [ $a == $b ]
then
echo "a be equal to b"
else
echo "a It's not equal to b"
fi

4 Boolean operator
4.1 grammar
| Operator | explain | give an example |
|---|---|---|
| ! | Non operation , Take the opposite , Expression for true Then return to false, Otherwise return to true. | [ ! expression ] Take the opposite . |
| -o | or Or operations , There is an expression for true Then return to true. | [ expression 1 -o expression 2 ] |
| -a | and And operation , Both expressions are true To return to true. | [ expression 1 -a expression 2 ] |
4.2 demo
#!/bin/bash
a=1 b=2
if [ $a -lt 2 -a $b -gt 10 ]
then
echo "$a Less than 2 And $b Greater than 10 : return true"
else
echo "$a Less than 2 And $b Greater than 10 : return false"
fi
if [ $a -lt 10 -o $b -gt 10 ]
then
echo "$a Less than 10 or $b Greater than 10 : return true"
else
echo "$a Less than 10 or $b Greater than 10 : return false"
fi
if [ ! $a -gt $b ]
then
echo "$a Greater than $b Take the opposite : return true"
else
echo "$a Greater than $b Take the opposite : return false"
fi

5 Logical operators
5.1 command
| Operator | explain | give an example |
|---|---|---|
| && | Logical AND | [[ expression 1 && expression 2 ]] |
| || | Logical OR | [[ expression 1 || expression 2 ]] |
| ! | Logic is not | [[ ! expression ]] |
Be careful
Use
&&and||The operator of must be placed in[[]]or(())In order to be effective , Otherwise, the report will be wrong
!Can be used in[],[[]]in , Not in (())
5.2 demo
#!/bin/bash
a=1 b=2
if [[ $a -lt 10 && $b -gt 10 ]]
then
echo " return true"
else
echo " return false"
fi
if [[ $a -lt 10 || $b -gt 10 ]]
then
echo " return true"
else
echo " return false"
fi

边栏推荐
- 超酷汇编教程-- 简明x86汇编语言教程(1)
- 数据采集系统网关采集工厂效率
- As a software testing engineer, how do you think to ensure software quality?
- 使用KVM虚拟化部署EVE-NG
- [open source Hongmeng system display] the rk3568 development board is equipped with openharmony 3.1 release
- Les neuf caractéristiques de la parole et les neuf temps en anglais
- Discuz copy today's headlines template /discuz news and information business GBK template
- Implementation of a small book system
- Qt内存映射
- 【Proteus仿真】51单片机+DS1302+lcd1602显示
猜你喜欢

Settings the PC must be turned on

shell 数组

Mise en place d'un Cluster kubernets avec plusieurs serveurs Cloud
![[proteus simulation] 51 MCU +ds1302+lcd1602 display](/img/02/9644415b72c330afa15060d81d4cbc.png)
[proteus simulation] 51 MCU +ds1302+lcd1602 display

On the realization of guessing numbers game

Numpy库使用入门

Network remote access using raspberry pie

shell 变量 入门

Today in history: Netease was founded; The first consumer electronics exhibition was held; The first webcast in the world

Beego--- notes
随机推荐
Gorm---- Association query
Insight into heap and stack stored in new string() /string() in JS
Regular match the phone number and replace the fourth to seventh digits of the phone number with****
Openstack -- creating virtual machines for Nova source code analysis
Discriminative v.s.Generative
Vscode--- format setting configuration
Untiy force refresh UI
JS prototype. slice. call(arguments); Convert pseudo array to array
None of the MLIR Optimization Passes are enabled (registered 2)解决办法
Realization of neural networks with numpy
OpenStack学习笔记(一)
【Proteus仿真】51单片机+DS1302+lcd1602显示
完整详细的汇编实验报告
On the compatibility of the disabled attribute of input on the mobile terminal
测一测你的挣钱能力有多强?未来的你将从事什么职业?
解决报错:Creating window glfw ERROR: GLEW initalization error: Missing GL version
电脑必须打开的设置
Table de hachage, conflit de hachage
‘nvidia-smi‘ 不是内部或外部命令,也不是可运行的程序或批处理文件
多台云服务器的 Kubernetes 集群搭建