In this article we learned how to use and define variables. According to the urine sex, the general next is to learn the basic data types of operations.
Yes, this is still a cliché to talk about this boring but must learn basic data types of operations.
Basic data type Operation operators
symbols |
Semantics |
Description |
+ |
Add |
10+10, the result is 20 |
- |
Reducing |
10-3, the result is 7 |
* |
By |
10*2, the result is 20 |
/ |
Except |
10/3, the result is 3 (take an integer) |
% |
Seeking redundancy |
10%3, the result is 1 (take the remainder) |
== |
Judging if they are equal |
Two numbers equal return 1, otherwise 0 |
!= |
Judging whether it's unequal |
Both numbers return 1, otherwise 0 |
> |
Greater than |
The former is greater than the latter return 1, otherwise 0 |
>= |
Greater than or equal to |
The former is greater than or equal to the latter return 1, otherwise 0 |
< |
Less than |
The former is less than the latter returns 1, otherwise 0 |
<= |
Less than or equal to |
The former is less than or equal to the latter return 1, otherwise 0 |
There is nothing special about the above operator compared to other languages.
In the shell, there are two main types of operations for the basic data type, integer and floating-point (fractional) operations. Here's a look at these two types of operations:
Integer arithmetic
In the shell, there are two ways to implement integer arithmetic, one is to use the expr command, and the other is to do it by square brackets ($[]). Let's look at the following separately:
Expr
#!/bin/bash# Output 13exprTen+3#输出10+3ExprTen+3#输出7exprTen-3#输出30exprTen\*3#输出3exprTen/3#输出1exprTen%3#将计算结果赋值给变量num1=$ (exprTen%3) #将计算结果赋值给变量num2= ' ExprTen%3`
Add: (greater than less than compare plus escape, or will < do the redirect symbol)
Attention:
In the above multiplication (*), we use a backslash () to escape, otherwise it will be an error.
The operator must have a space before and after it, or it will be returned directly as a string.
If you want to save the results of the calculation to a variable, you need to replace the command with the two methods ($ () or ') that we talked about in the previous article.
All these signs make people vomit. Fortunately there is another way to do it, and that is the square brackets to see next.
square brackets ($[])
#!/bin/Bashnum1=Tennum2=3#输出num1+ num2= -Echo"NUM1 + num2=$[$num 1 + $num 2]"#输出num1+num2= -Echo"num1+num2=$[$num 1+ $num 2]"#输出num1-Num2=7Echo"num1-num2=$[$num 1-$num 2]"#输出num1* num2= -Echo"NUM1 * num2=$[$num 1 * $num 2]"#输出num1> num2=1Echo"num1 > num2=$[$num 1 > $num 2]"#输出num1< num2=0Echo"NUM1 < num2=$[$num 1 < $num 2]"#将运算结果赋值给变量, Output num3=3num3=$[$num 1/$num 2]echo"num3= $num 3"
See this operation, and then go back to see expr, is not feel to ascend, finally normal. The few caveats of expr are not a thing here. So, if the yaotu is simple, still use this way.
Floating point Arithmetic
In the shell, doing floating-point arithmetic is usually done with Bash's calculator (BC). In shell scripts, the general way we use them is:
variable=$ (echo "options; Expression "| bc
Options is an option for BC, for example: You can set the number of decimal digits reserved by using scale. Specific parameters, can be viewed by man BC
expression is our specific expressions, such as 10 * 3
" | " This symbol, for people familiar with the Linux system, this is more familiar. It is called the pipeline, the reason is called the pipeline, in fact, you can think of it as a water pipe, the water pipe at the end of access to the previous command return results, one access to the next command. Indicates that the execution result of the previous command is entered as a parameter of the latter command. Above, which means that our expression is entered as a parameter of the BC.
#!/bin/bash#表示 10/3, 保留2位小数,将结果赋值给了num, 输出3.33num=$(echo "scale=2; 10 / 3" | bc)echo $num
Summary
This article is rather boring, briefly introduces the basic shell operators and their operations. Shell operations are mainly divided into integer and floating-point operations. The integral type is implemented in two ways, floating point is achieved by using Bash's built-in calculator (BC).
Well, here we learn about the use of shell variables, as well as the calculation of basic data type variables! Pretty simple.
Shell Programming (ii)