当前位置:网站首页>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
边栏推荐
- What is the difference between ERP production management and MES management system?
- 数据库基础——规范化、关系模式
- Principle and configuration of MPLS
- Install iptables services and open ports
- ctfshow web 1-2
- 建立MES系统,需要注意什么?能给企业带来什么好处?
- Hands on learning and deep learning -- simple implementation of softmax regression
- JVM learning notes: three local method interfaces and execution engines
- Webrtc adding third-party libraries
- Seurat package addmodulescore is used for bulk RNA SEQ data
猜你喜欢

(p36-p39) right value and right value reference, role and use of right value reference, derivation of undetermined reference type, and transfer of right value reference

(p25-p26) three details of non range based for loop and range based for loop

如何理解APS系统的生产排程?

JVM学习笔记:垃圾回收机制

Engineers learn music theory (II) scale and tendency

This article is required for the popularization of super complete MES system knowledge

Webrtc series - mobile terminal hardware coding supports simulcast

Production scheduling status of manufacturing enterprises and solutions of APS system

Oracle installation details (verification)

Vision transformer | arXiv 2205 - TRT vit vision transformer for tensorrt
随机推荐
Configuration and principle of MSTP
Database foundation -- normalization and relational schema
动态线段树leetcode.699
Centso8 installing mysql8.0 (Part 2)
Regular expressions in JS
What is the quality traceability function of MES system pursuing?
Where does the driving force of MES system come from? What problems should be paid attention to in model selection?
What is the beauty of MES equipment management for enterprises?
Installation of Shengxin R package
MPLS的原理与配置
Calling stored procedures in mysql, definition of variables,
Callback webrtc underlying logs to the application layer
Jump to an interface at a specified time. (Web Development)
Hands on deep learning -- weight decay and code implementation
Arrays in JS
JVM learning notes: garbage collection mechanism
Why should enterprises implement MES? What are the specific operating procedures?
Call method and apply method
(P13) use of final keyword
A brief summary of C language printf output integer formatter