当前位置:网站首页>Detailed explanation of calculation commands in shell (expr, (()), $[], let, bc )

Detailed explanation of calculation commands in shell (expr, (()), $[], let, bc )

2022-08-02 08:37:00 DI walking to a

目录

一、expr命令

1、整数运算(Note that need space,需要加"\"Don't let special characters to escape)

1、Obtain the calculation results to the grammar of the new variable

2、字符串语法 

1、Calculate the length of the string syntax

2、Intercept string syntax

3、For the first time a character in the string position

4、正则表达式匹配1语法

5、正则表达式匹配2语法,功能和1一样,语法比1简单

二、计算命令 :(()) 命令

1、作用   

2、介绍

3、语法

         4、用法

① To double the inside of the parentheses variable assignment

②Give double bracket outside variable assignment

③Budget in logic operator to use

三、let 命令详解

四、$[] 命令

1、功能

能够使用$[ ]For integer math

2、语法

3、用法

五、bc 命令

1、功能:

Ⅰ:bcIn the interactive mathematics

2、语法

3、内置变量

Ⅱ: bc在shellIn the interactivebc管道运算

1、语法

2、将bcThe results of calculation toshell变量 

Ⅲ: bc在shellIn the interactive input redirection arithmetic

1、作用

2、语法

总结: 


一、expr命令

expr (evaluate expressions的缩写)."表达式求值".Shell expr是一个功能强大.并且比较复杂的命令,它除了可以实现整数计算,还可以结合一些选项对字符串进行处理,例如计算字符串长度、字符串比较、字符串匹配、字符串提取等.

空格隔开每个项
\(反斜杠)放在 shell 特定的字符前面
“”(引号)对包含空格和其他特殊字符的字符串要用引号括起来

1、整数运算(Note that need space,需要加"\"Don't let special characters to escape)

1、Obtain the calculation results to the grammar of the new variable

可以使用$()和 Two good back”``“  表示.

2、字符串语法 

1、Calculate the length of the string syntax

语法:expr length  

2、Intercept string syntax

语法:expr substr ”字符串“ start end

#start 截取字符串的起始位置,从1开始(Note number from the start,而不是0开始)

#end截取字符串的结束位置,Contains the location also be intercepted

 (start=1;end=2)

3、For the first time a character in the string position

语法:expr index 被查找的字符串  需要查找的字符

4、正则表达式匹配1语法

语法: expr match  字符串  正则表达式

#Regular expressions by default with“^”,表示已什么开头

#The return value to meet,匹配字符长度,否则返回0

#正则表达式“."(点)代表任意一个字符

#Regular expression in the wildcard“*”(星号)On behalf of the characters can appear in front of0次到多次

#“.*m”(点心m):匹配字符串中mThe length of the string in front of(Begin with arbitrary length characters tomEnd and includesm,匹配m之前的长度.It is concluded that the length of the string matching)

5、正则表达式匹配2语法,功能和1一样,语法比1简单

语法:expr 字符串 :正则表达式

#Regular expressions by default with“^”,表示已什么开头

#The return value to meet,匹配字符长度,否则返回0

 expr  小结:

1.整数计算:

      expr   整数运算表达式

2字符串操作:

expr length 字符串     :获取字符串的长度

expr substr  字符串   start   end   :      截取字符串

expr index   字符串  查找的字符       :查找字符在字符串中第一次出现的位置,位置从1开始的

(expr match  字符串    正则表达式);(expr   字符串 : 正则表达式)

二、计算命令 :(()) 命令

1、作用   

 (())When writing in arithmetic expressions,With or without Spaces between each other can be,Meet with special characters also don't need to add“\”

能够使用(())For integer math

2、介绍

双小括号(()),用于进行数学运算表达式的执行,Put mathematical expression in the((和))之间.可以使用$获取(())表达式命令的结果,这和使用$Get a variable's value is the same.

3、语法

((整数运算表达式))

4、用法

① To double the inside of the parentheses variable assignment

((a=1+6))     After completion of the calculation can give the variable assignment.

((b=a-1))      把a-1的计算结果赋值给b,The variables ina前面不需要加$,(())With automatic parsing double brackets                         The function of the variable.

②Give double bracket outside variable assignment

a=$((1+6))   

b=$((a-1))

c=$((a+b))

解析:可以在(())前面加上S符号获取(())命令的执行结果,To obtain the entire value of the expression.以c=$((a+b))为例,即将a+b这个表达式的运算结果赋值给变量c.注意,如果c=((a+b)这样的写法是错误的,不加$就不能取得表达式的结果.

③Budget in logic operator to use

((a>7 && b==c))    :   (())也可以进行逻辑运算,在if语句中常会使用逻辑运算.

echo $((a+10))      :   需要立即输出表达式的运算结果时,可以在(())前面加$符号

((a=3+5,b=a+10))  :   For multiple expression is calculated at the same time,多表表达式使用"∵"号隔开

需求:

1、计算1+6并赋值给变量a

2、计算变量a-1赋值给变量b

3、计算变量a+变量b赋值给变量c

4、打印变量a,变量b,变量c的值

5、$赋值写法

6、(())Multiple expression computing assignment

7、 echo输出直接使用(())

8、 (())Used for logical expression inif中使用

(()) 用法 小结:

1、Brackets assignment:((变量名=整数表达式))

2、Outside the parentheses assignment:变量名=$((整数表达式))

3、More expression assignment:((变量名1=整数表达式1,变量名2=整数表达式2...))

4、与ifConditional sentence with: if((整数表达式))

三、let 命令详解

功能:能够使用letFor integer math assignment

letCommand and double parentheses(()),In terms of mathematical calculation function,但是没有(())的功能强大,letCan only be used for assignment to calculate,不能直接输出,不可以和ifConditional used together.

语法:let 赋值表达式

1、Grammatical function is equivalent to((表达式)),letAt the back of the integer arithmetic expressions variables do not need to add$,Before the special characters also don't need to add“\”

2、Use Spaces between multiple expression 而不是","号

3、对于类似let a+b这样的写法,Shell虽然计算了a+b的值,但却将结果丢弃,如果echo let a+b 会直接输出字符串 a+b;若不想这样.可以使用1et summa+b将a+b的结果保存在变量sum中

示例:Normal integer arithmetic

变量++和++变量的区别

Variable value since the increase+2的表示方法

Multiplication method

let 命令小结:

1、letDigital integral calculation usage

2、作用:用于赋值,Is the most concise integer arithmetic assignment command computing assignment usage: let变量名=整数运算表达式

3、Multiple expression computing assignment usage: let  变量名1=整数运算表达式1  变量名2=整数运算表达式2  ...

四、$[] 命令

1、功能

能够使用$[ ]For integer math

和(())、let命令类似,$[ ]Only integer arithmetic is also.But only for a single expression calculation is evaluated with the output,Not a number belong to.

2、语法

格式:$[表达式]

①$[ ]The expression to calculate,And the results obtained

②The interior of the expression is not can be assigned to a variable without(())功能强大

3、用法

五、bc 命令

1、功能:

Complete integer and decimal arithmetic

①能够在bcIn the interactive mathematics

②能够在shellIn the interactivebc管道运算

③能够在shellThe china-africa interactive input redirectionbc运算

Ⅰ:bcIn the interactive mathematics

2、语法

语法一(Used as a calculator system)

格式:bc [选项] [参数]

 You can also calculate the content of the file

3、内置变量

scale :指定精度,To reserve calculation results specified decimal;默认为0,That does not use the decimal part keep only integer bit.

Ⅱ: bc在shellIn the interactivebc管道运算

1、语法

echo "表达式" | bc [选项]

2、将bcThe results of calculation toshell变量 

第一种方式:

A=$(echo "表达式" | bc [选项])

第二种方式:反引号

A=`echo "表达式" | bc [选项]`   

两种方式区别:``The quotation is alllinuxSystems support the way.兼容性较好,But easily confused with quotation marks

$()不是所有linuxSystems support the way.兼容性较差,But it is not easy to confused

Ⅲ: bc在shellIn the interactive input redirection arithmetic

1、作用

 Calculation expression output tobc去执行,Characteristics similar to the input file,Can enter the multi-line expression,更加清晰

2、语法

第一种方式:

A=`bc << EOF

The first line expression

The second line expression

The third line expression

......................

EOF

` (The quotation marks the end)

第二种方式:

A=$(bc << EOF

The first line expression

The second line expression

The third line expression

......................

EOF

) 括号结束

总结: 

let Assignment is the most convenient    

(()) Output directly evaluated the most convenient time for you,At the same time and support for multiple expression

expr     优点:可以直接输出

            缺点:计算表达式里面引用变量使用$,Special characters need to escape only calculate an expression

(())    优点:直接输出,里面直接使用变量名,特殊字符不需要转义,多个表达式赋值,Can be carried out more expression               同时i计算,使用”,“互相分开

         缺点:The need for value can be output

let     优点:赋值简单,特殊字符不需要转义,Can be carried out more expression at the same timei计算,Use Spaces to separate each other

         缺点:不能直接输出
$[ ]    优点:特殊字符不需要转义

          缺点: Can't more expressions and calculation

bc You can calculate the integer and decimal,After retain the decimal several,Only effective division

  

原网站

版权声明
本文为[DI walking to a]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/214/202208020802027940.html