当前位置:网站首页>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

边栏推荐
猜你喜欢

Related examples of data storage in memory

As a software testing engineer, how do you think to ensure software quality?

What are the red lines of open source that should not be trodden on?

Rust, le meilleur choix pour un programmeur de démarrer une entreprise?

shell 字符串变量

Mise en place d'un Cluster kubernets avec plusieurs serveurs Cloud

Getting started with numpy Library

Scope of ES6 variable

Classifier and cross entropy loss function

shell 变量 入门
随机推荐
Cesium learning notes
Classifier and cross entropy loss function
Prototype and prototype chain - constructor and instanceof
Insight into heap and stack stored in new string() /string() in JS
完整详细的汇编实验报告
Hash table, hash conflict
国信证券股票开户是安全的吗?
shell 数组
Deep parsing and implementation of redis stream advanced message queue [10000 words]
数据库中显示error1822,error1824
Explain the possible memory leaks caused by the handler at one time and the solutions | the developer said · dtalk
一次性讲清楚 Handler 可能导致的内存泄漏和解决办法 | 开发者说·DTalk
专家建议|8大措施加速你的创新职业规划和成长
When the input tag type is number, the input of E, e, -, + is blocked
Prototype relationship between constructor and instance (1)
Turtlebot+lms111+gmapping practice
网络远程访问的方式使用树莓派
Scope of ES6 variable
国信证券股票账户开户安全吗?请问。
历史上的今天:网易成立;首届消费电子展召开;世界上第一次网络直播