当前位置:网站首页>Shell basic syntax -- arithmetic operation
Shell basic syntax -- arithmetic operation
2022-06-12 08:35:00 【__ Wanbo__】
because shell Any data type is treated as string by default
So when performing arithmetic operations , Unlike other languages, it is not easy to add and subtract
Three arithmetic operation identifiers
(( )) # Recommended , Because use (()) Enclosed expression root C The language and grammar are the same , In line with daily use habits
$[] # The result of the calculation is either assigned to a variable , Or use echo Print out
expr # The calculation results are automatically printed to the terminal
1. (( )) Arithmetic operation identifier
a=123
b=456
# And C Arithmetic operations are used in the same way in languages
# The internal variables in double parentheses may not be preceded by $ operator
# + You can add a space before and after the sign , You can also leave no spaces
# The return value is directly obtained by the equal sign
sum=$(($a+$b))
sum=$((a + b))
echo $sum
# Self increase and self decrease in double parentheses , There is no need to assign again
((a++))
((a--))
# The comma operator , ++ And -- All operations and C Same language
tt=$((a++, ++b))
# Power operation
sqrt=$((3**2))
# You can do complex operations in double parentheses
# (()) Variables in are declared automatically , There is no need to declare separately on the outside , After calculation, it can be used freely in the following
for ((i=0; i<=100; i++))
{
((sum += i))
}
2. $[ ] Arithmetic operation identifier
# $[] There must be a variable to accept the return value , Or use echo Print
# Otherwise it will be reported command not found error
a=100
b=200
# And C Arithmetic operations are used in the same way in languages
# stay [] You can add $ operator , Or you can skip it
# stay [] Can be found in + Add a space around the sign , Or you can skip it
ret=$[a + b]
# stay $[] Complex operations are not allowed in , Can't write like for $[i=0; i<100; i++] In this way for Loop statement
# $[] Multiple expressions in are separated by commas , The following expression will operate step by step from left to right according to the comma , The final value is assigned to ret
a=100
b=2
ret=$[a++, ++a, a**b]
# The output is : 102 * 102 = 10404
3. expr Arithmetic operation identifier
# expr It is the most regular and most arithmetic operation identifier , Automatically print after operation , It will not print automatically after using the command displacement
# + There must be spaces at both ends of operators such as sign , Without spaces, it will be recognized as a string
# When using variables , Variables must be preceded by $ operator
a=100
b=200
# take expr When the operation result is assigned to a variable , To add a command substitution character
sum=`expr $a + $b`
# expr perform * Normal operation time , Think * The number is a wildcard , So you have to escape when you multiply
# Parentheses also need to be escaped , And the expression in parentheses should be filled with spaces
a=100
b=200
ret=`expr $a \* \( $b + 2 \)`
echo $ret # Output 20200
# expr Cannot perform power operation
# Sum up , expr The use of is simply useless , It's a lot of trouble , So it's not used in general expr Do arithmetic operations , Its advantage is string manipulation
# The correct posture is as follows :
# 1. Find the length of the string
expr length $str # There must be no spaces in the string
# 2. lookup character Position in string
expr index $str "a" # There must be no spaces in the string
# 3. Intercepting string , Intercept from the starting position n Characters
expr substr $str 3 4 # There must be no spaces in the string
# 4. Returns the number of characters that are regular matches , Otherwise return to 0
expr match $str "^abc$" # There must be no spaces in the string
边栏推荐
- At present, MES is widely used. Why are there few APS scheduling systems? Why?
- Principle and configuration of MPLS
- Call method and apply method
- 在Tensorflow中把Tensor转换为ndarray时,循环中不断调用run或者eval函数,代码运行越来越慢!
- Jump to an interface at a specified time. (Web Development)
- (P13) use of final keyword
- Installation series of ROS system (I): installation steps
- Detailed explanation of private, public and interface attributes in cmake
- Py&GO编程技巧篇:逻辑控制避免if else
- (p21-p24) unified data initialization method: List initialization, initializing objects of non aggregate type with initialization list, initializer_ Use of Lisy template class
猜你喜欢

Bean的作用域

Asp Net project add log function

ctfshow web 1-2

FDA reviewers say Moderna covid vaccine is safe and effective for children under 5 years of age

What is the MES system? What is the operation process of MES system?

Hands on deep learning -- image classification dataset fashion MNIST

(p15-p16) optimization of the right angle bracket of the template and the default template parameters of the function template

Error: clear the history in the search box in the website?

【进阶指针一】字符数组&数组指针&指针数组

JVM learning notes: three local method interfaces and execution engines
随机推荐
Engineers learn music theory (II) scale and tendency
如何理解APS系统的生产排程?
记Date类型的一次踩坑
根据有效期显示距离当前还剩多少天有效期
工厂的生产效益,MES系统如何提供?
2022.6.11-----leetcode.926
动态线段树leetcode.699
MES helps enterprises to transform intelligently and improve the transparency of enterprise production
Hands on deep learning -- Introduction to linear regression model
Webrtc adding third-party libraries
ctfshow web 1-2
APS究竟是什么系统呢?看完文章你就知道了
Regular expressions in JS
Scope of bean
Calling stored procedures in mysql, definition of variables,
(p33-p35) lambda expression syntax, precautions for lambda expression, essence of lambda expression
网站Colab与Kaggle
FDA reviewers say Moderna covid vaccine is safe and effective for children under 5 years of age
Ankerui fire emergency lighting and evacuation indication system
Py & go programming skills: logic control to avoid if else