Linux-shell- arithmetic, BC, DC, {expr () () and []}

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

You are how to do arithmetic under Linux? Is it right? Still use expr? You will say I will bc it.!

Gossip not pull, doing!

expr

Expr in use should pay attention to some writing, such as that between quantity and the operation symbols in the expression space and some operations need to escape, there is one thing to remember, expr is only applicable to integer arithmetic!

Expression

The expr help document on the part of the expression is as follows:

  ARG1 | ARG2 if the ARG1 value is not 0 or is empty, it returns ARG1, otherwise it returns ARG2
 
  ARG1 & ARG2 if both values are not 0 or is empty, it returns ARG1, otherwise it returns 0
 
  ARG1 < ARG2 ARG1 is less than ARG2
  ARG1 <= ARG2 ARG1 is less than or equal to ARG2
  ARG1 = ARG2 ARG1 is equal to ARG2
  ARG1 != ARG2 ARG1 is not equal to ARG2
  ARG1 >= ARG2 ARG1 is greater than or equal to ARG2
  ARG1 > ARG2 ARG1 is greater than ARG2
 
  ARG1 + ARG2 calculation of ARG1 and ARG2 sum
  ARG1 - ARG2 calculation of ARG1 and ARG2 subtraction difference
 
  ARG1 * ARG2 calculation of ARG1 multiplied by ARG2 product
  ARG1 / ARG2 calculation of ARG1 and ARG2 by taking
  ARG1 % Calculation of ARG2 ARG1 and ARG2 divide the remainder

This part that most everyone to use, but also for these understanding, we use an expression to show:

$expr 9 + 8 - 7 \* 6 / 5 + \( 4 - 3 \) \* 2
11

The results that you already know the calculation rules of expr, and the mathematical expressions of our understanding of the daily highest priority, parentheses, and then “ *” “ /”,, and each number or symbol are separated by spaces, the result is an integer.

Character string

The expr can also operate on strings:

  String expression is equal to match"String: expression"
  The substr string offset length replacing substrings, offset value from the 1 In terms of
  The index string of characters found in the string of characters place to establish a subscript, or standard 0
  The length string string length

1)match

Expr expr match  $string substring command of substring string matching in the string string (substring string can be a regular expression), and then return to the length of the substring string matching, and if they cannot find it returns 0.

Below we to examples:

┌[2013-08-24/7.18  15:00:01]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$str="123 456 789"
┌[2013-08-24/7.18  15:00:30]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$expr match "$str" .*5
6

The.*5 matching 6 characters.

2)substr

In shell can be carried out on the string character string extraction with {string:position} and {string:position:length}. The first is from the position position until the end of the string to extract, second from the position position to extract substring of length length. And using expr in expr substr $string $position $length also can realize the function.

Example:

┌[2013-08-24/7.18  15:19:17]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$str="123 456 789"
┌[2013-08-24/7.18  15:19:31]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo ${str:5}
56 789
┌[2013-08-24/7.18  15:19:59]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo ${str:5:3}
56
┌[2013-08-24/7.18  15:20:07]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$expr substr "$str" 5 3
456

It can be seen from the {string:position} and {string:position:length} counting begins at 0, while expr substr $string $position $length from the beginning of 1.

3)index

Expr expr index $string substring substring character index command function to find the position of the first occurrence of the string $string, and if they cannot find the expr index returns 0. Note that it is not matching character string.

Example:

┌[2013-08-24/7.18  15:35:19]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$str="123 456 789"
┌[2013-08-24/7.18  15:37:02]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$expr index "$str" b
0
┌[2013-08-24/7.18  15:37:08]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$expr index "$str" 9
11

4)length

To calculate the length of the string. We can use awk length (s) were calculated. We can also use echo echo ${#string} to calculate, of course, also can be in expr expr length $string to calculate the length of the string.

┌[2013-08-24/7.18  15:39:39]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$str="123 456 789"
┌[2013-08-24/7.18  15:39:52]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo ${#str}
11
┌[2013-08-24/7.18  15:39:57]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$expr length "$str"
11

bc

The key to the!

BC is a calculation language an arbitrary precision, attention is a kind of language, it provides some grammatical structures, such as conditional, circulation, can be said to be very powerful, but I actually didn't find   need the use of occasions;. Another use is for hexadecimal conversion.

Integer arithmetic support above we introduce expr, but for floating-point arithmetic is incapable of action, but expr cannot index calculation, and have BC these are no longer under the words.

Parameters

We first come to find several parameters are useful:

-I forced interaction model, 

-L uses BC's built-in libraries, there are some math library BC, very useful for triangle calculation, 

The -q into the BC interaction mode no longer output version redundant information. 

Special variables

ibase, Obase for hexadecimal conversion, IBASE is to enter the hexadecimal, obase is the output of the system, the default is the decimal,

Scale fractional reserve figures, default reserves 0.

Interactive mode

Input BC interaction mode and can enter the BC language directly on the shell command line.

$bc -l -q
4/3                           /*Default reserved integer precision is not specified*/
1
scale=5                   /*The specified precision is 5*/
4/3
1.33333
ibase=2                    /*Input mechanism specifies hexadecimal conversion to binary, output is the default for the decimal*/
1010
10
4^2                           /*Index calculation, note: index can not float*/
16
4*a(1)                        /*Calculation of &pi value, a; () is a function: arctan (), well, teachers have been eaten by the dog, π value is equal to four times arctan (1).? */
3.14159265358979323844
quit                            /*Sign out*/

In non interactive mode

BC can also be used for non interactive operation, method is used together with echo.

┌[2013-08-24/7.18  18:42:27]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo "scale=5;9+8-7*6/5^2"|bc          /*Priority^ > *,> +,- */
15.32000
┌[2013-08-24/7.18  18:45:35]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo "s(2/3*a(1))"|bc -l                         /*Remember Sina (30°) is equal to 0.5? The Emperor! ^_^*/
.49999999999999999998
┌[2013-08-24/7.18  18:49:13]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo "scale=5;sqrt(15)"|bc                   /*Sqrt*/
3.87298
┌[2013-08-24/7.18  18:49:18]
├[14+1][~]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo "ibase=16;obase=2;ABC"|bc
101010111100

More refer to the man documentation!

dc

You may have known this command, you may not know. Compared to DC and BC to complex, but simple operation is relatively simple. Simple to say that DC is always push operation, like the BC, it can also interact with, or together with echo with the use of.

It also supports floating point arithmetic.

But now I don't think this pressure stack arithmetic have something.

Interactive mode

┌[2013-09-16/8.12  20:33:53]
├[7+10][~/shell]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$dc
2
3-
p      /*Output(3 - 2)*/
-1
4
*
p          /*Output(-1 * 4)*/
-4
2
/
p       /*Output(-4 / 2)*/
-2
3.4
+
p      /*Output(-2 + 3.4)*/
1.4
4
d        /*Copy the top values*/
*
p         /*Output(4 * 4)
16
q         /Sign out*/

There are other commands such as:

C remove the stack

The replication of D value at the top of the stack

The output of the P stack top value

Q exit interaction model

There are other can refer to the corresponding man document.

In non interactive mode

A formula to make you look almost the same.

┌[2013-09-16/8.12  20:47:43]
├[7+10][~/shell]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo "4 3 * 2 + 1 -p"|dc
13

Formula is: (4 * 3 + 2 - 1).

Is it right? Is very simple!

(( )) & [ ]

The two in the shell in the more common, the two and expr commands are similar, but also for the integer calculation.

Operation symbol they support are as follows:

 |    Bit or
+ ||   If the front and the back neither is 0, it returns 1, otherwise it returns 0
 &  And the
+ &&  If the former is 0, no longer in the latter process, or to the latter, the latter is not 0 returns 1
 <
 <=
 ==
 !=
 >=
 > 
 + 
 - 
 * 
 / 
 % 

The two operator with the + is the operator shell support.

The two of the advantages with expr is: operation symbols are not need to escape.

We just use shell in order those people not used to do the same thing, but don't forget to () () and [] shell is common, but very practical, but it is not your usual!

Example:

┌[2013-09-16/8.12  20:47:51]
├[7+10][~/shell]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo $(( 2 + 5 ))
7
┌[2013-09-16/8.12  21:11:14]
├[7+10][~/shell]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo $(( 2 * 5 ))
10
┌[2013-09-16/8.12  21:11:19]
├[7+10][~/shell]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo $(( 2 - 5 ))
-3
┌[2013-09-16/8.12  21:11:23]
├[7+10][~/shell]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo $(( 2 % 5 ))
2
┌[2013-09-16/8.12  21:11:29]
├[7+10][~/shell]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo $[ 2 % 5 ]
2
┌[2013-09-16/8.12  21:11:45]
├[7+10][~/shell]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo $[ 2 - 5 ]
-3
┌[2013-09-16/8.12  21:11:50]
├[7+10][~/shell]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo $[ 2 * 5 ]
10
┌[2013-09-16/8.12  21:11:55]
├[7+10][~/shell]
└[snowsolf@Ubuntu-LTS-1 ╰_╯]$echo $[ 2 + 5 ]
7

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Darcy at November 08, 2013 - 1:47 AM