Friday, April 10, 2009

Arithmetic operation and expression in shell script with expr

With expr you can evaluate expression and can do arithmatic operations.
The syntax of using expr is below:
expr operand1 operator operand2

Note that there is a space between the operator and the both operands.

The list of the operator is discussed below.
1)"|": Usage syntax is ARG1 | ARG2 and result is the ARG1 if it is neither null nor 0, otherwise reult is ARG2.
Example:

Tahaa2:/home/arju/test# expr 10 \| 0
10
Tahaa2:/home/arju/test# expr 0 \| 12
12

Note that as | is a special character so escape character (\) is needed before to use them.

2)"&": Usage syntax is ARG1 & ARG2 and result is the ARG1 if neither argument is null or 0, otherwise 0.
Example:

Tahaa2:~# expr "" \& 12
0
Tahaa2:~# expr 7 \& 12
7

Note that as & is a special character so escape character (\) is needed before to use them.

3)"<" : Usage syntax is ARG1 < ARG2 and result is 1 if ARG1 is less than ARG2, otherwise result is 0.

Tahaa2:~# expr 7 \< 12
1
Tahaa2:~# expr 12 \< 7
0

4)"<=": Usage syntax is ARG1 <= ARG2 and result is 1 if ARG1 is less than or equal to ARG2, otherwise result is 0.

Tahaa2:~# expr 0 \<= 0
1
Tahaa2:~# expr 0 \<= -8
0

5)"=" : Usage syntax is ARG1 = ARG2 and result is 1 if ARG1 is equal to ARG2, otherwise result is 0.

Tahaa2:~# expr 0 = 1
0
Tahaa2:~# expr 0 = 0
1

6)"!=" : Usage syntax is ARG1 != ARG2 and result is 1 if ARG1 is not equal to ARG2, otherwise result is 0.

Tahaa2:~# expr "0" != 0
0
Tahaa2:~# expr "0" != 10
1

7)">=" : Usage syntax is ARG1 >= ARG2 and result is 1 if ARG1 is greater than or equal to ARG2, otherwise result is 0.
Example:

Tahaa2:~# expr 23 \>= 24
0
Tahaa2:~# expr 1 \>= 1
1

8)">" : Usage syntax is ARG1 > ARG2 and result is 1 if ARG1 is greater than ARG2, otherwise result is 1.
Example:

Tahaa2:~# expr 23 \> 24
0
Tahaa2:~# expr 1 \> -1
1


9)"+" : Arithmetic sum of the two operands.

Tahaa2:/home/arju/test# expr 5 + 6
11

10)"-" : Arithmetic difference of the two operands.

Tahaa2:/home/arju/test# expr 5 - 6
-1

11)"*" : Arithmetic product of the two operands.

Tahaa2:~# expr 7 \* 8
56

12)"/" : Usage syntax is ARG1 / ARG2 and result is the arithmetic quotient of ARG1 divided by ARG2.

Tahaa2:~# expr 7 / 8
0
Tahaa2:~# expr 78 / 8
9

13)"%" : Usage syntax is ARG1 % ARG2 and result is the arithmetic remainder of ARG1 divided by ARG2.

Tahaa2:~# expr 78 % 8
6

14)length STRING : Returns the length of the string.
Example:

Tahaa2:~# expr length " This is normal Text"
20

15)index STRING CHARS: Return the indexing number of the CHARS in STRING if found, otherwise returns 0. Note that numbering start from 1. If there is multiple characters inside STRING of CHARS then first index number is returned. And also first characters of CHARS is evaluated.
Example:

Tahaa2:~# expr index "Arju, Robert, Richard" "R"
7

first one of R position is return.

Tahaa2:~# expr index "Arju, Robert, Richard" "Ri"
7

Position of R is returned, not of i.

Tahaa2:~# expr index "Arju, Robert, Richard" "c"
17
Tahaa2:~# expr index "Arju, Robert, Richard" ","
5

16) substr STRING POS LENGTH : It returns substring of STRING, starting from POS and string length of LENGTH. Note that position starts from 1.

Tahaa2:~# expr substr "Arju, Robert, Richard" 6 7
Robert

In the example from the string, from position 6 (i.e space) upto 7 lengths of characters are returned.

17)match STRING REGEXP : Anchored pattern match of REGEXP in STRING. Can also be expressed as STRING : REGEXP
Example:

Tahaa2:~# expr match "Arju, Robert, Richard" "Arju "
0
Tahaa2:~# expr match "Arju, Robert, Richard" "Arju,"
5

Related Documents
http://arjudba.blogspot.com/2009/04/system-notes-org-how-to-copy-and-paste.html
http://arjudba.blogspot.com/2009/04/what-is-kernel-shell-shell-script.html
http://arjudba.blogspot.com/2009/04/basic-steps-to-write-shell-script.html

No comments:

Post a Comment