shell 08 (shell calculation command)

1. (()) command

Double parentheses (( )) are used to perform mathematical operation expressions. Place mathematical operation expressions between (())

You can use $ to get the result of the (()) expression command, which is the same as using $ to get the value of a variable.

((表达式))

2. let command

The let command has the same function as the double parentheses (()) in numerical calculations, but it is not as powerful as (( )). The let command can only be used for assignment calculations , cannot be output directly, and cannot be used with conditional judgments.

let 赋值表达式

1. The syntax function is equivalent to ((expression))
2. Use spaces between multiple expressions, not ","
3. For writing methods like let a+b

  • Although the Shell calculates the value of a+b, it discards the result;
  • If echo let a+b will directly output the string a+b;
  • If you don't want to do this, you can use let sum=a+b to save the result of a+b in the variable sum.

is the most concise integer arithmetic assignment command

Usage of calculation and assignment of multiple expressions: let variable name 1 = integer operation expression 1 variable name 2 = integer operation expression 2...

3. $[] command

Similar to the (()) and let commands, $[] can only perform integer operations. However, only a single expression can be evaluated and output.

$[表达式]


1.$ will calculate the expression and obtain the calculation result

2. Expressions cannot be assigned to variables

4. bc command

The Bash shell has built-in support for integer operations, but it does not support floating-point operations. The linux bc (basic calculator) command can easily perform floating-point operations .

  • The bc command is a simple calculator in Linux that can perform base conversion and calculation. The base systems that can be converted include hexadecimal, decimal, octal, binary, etc.
  • The arithmetic symbols that can be used include (+) addition, (-) subtraction, (*) multiplication, (/) division, () exponent, (%) remainder, etc.

4.1 Interactive mathematical operations in bc

grammar:

bc [options] [参数]

Case: Executing Computing Task Files

You can put the calculated expression into the taste.txt file and execute it together:

108*67+12345

58+2007*11

bc -q task.txt

4.2 Non-interactive bc pipeline operation

4.3 Non-interactive input redirection bc operation

Output calculation expressions to bc for execution. The characteristics are similar to inputting in files. You can enter multi-line expressions, which is more clear.

Guess you like

Origin blog.csdn.net/peng_258/article/details/132453591