Shell Programming (ii)

Source: Internet
Author: User
Tags arithmetic

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:

  1. In the above multiplication (*), we use a backslash () to escape, otherwise it will be an error.

  2. The operator must have a space before and after it, or it will be returned directly as a string.

  3. 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.