Control Mechanisms of the UNIX Shell - The expr command
(Page 4 of 4 )
When we evaluate variables, sometimes we would like to perform some basic arithmetic operations. From this, we will learn in the end whether or not a condition is active. The simplest command what deals with this is the expr. The expr can also perform some basic string operations.
The syntax of the expr is quite simple:
expr expression
The expression will print the result on the standard output. What can the expression be? The table below shows a list of these:
Expression
What does it do?
ARG1 | ARG2
ARG1 if that is not zero, otherwise ARG2
ARG1 & ARG2
ARG1 if neither is zero or zero length string, otherwise 0
ARG1 < ARG2
ARG1 is smaller than ARG2
ARG1 <= ARG2
ARG1 is smaller or equal than ARG2
ARG1 = ARG2
ARG1 is equal with ARG2
ARG1 != ARG2
ARG1 is not equal with ARG2
ARG1 >= ARG2
ARG1 is greater or equal than ARG2
ARG1 > ARG2
ARG1 is greater than ARG2
ARG1 + ARG2
Arithmetic sum
ARG1 - ARG2
Arithmetic subtraction
ARG1 * ARG2
Arithmetic multiplication
ARG1 / ARG2
Arithmetic divide
ARG1 % ARG2
Arithmetic modulo of ARG1/ARG2
STRING : REGEXP
string match from the first character of the STRING
match STRING REGEXP
The same as above
substr STRING POS LENGTH
returns from STRING a substring that start from POS and has a length of LENGTH
index STRING CHARS
Returns the index from STRING where any characters from CHARS can be found
length STRING
The length of the STRING
You have to bypass the meta-characters with the help of the backslash. You have to do so with the <,>, &, |, * ones.
A couple of examples:
expr 1 + 2
a= $( expr 2 > 3)
b= `expr 1 * 2`
You need to leave the spaces between the arguments and the operators. Otherwise, you will wake up with a beautiful error message. Here is how you can use the expr with the “if structure:”
if expr 2 < 3
then
echo Two is smaller than three
fi
This will be all for today. Next week we will cover data streams, and then we will learn a more advanced and complicated command to test conditions. This is the test command. Until then, start up your console, make sure you try out what you learned today, and remember that repetition is the mother of knowledge. Thank you for reading my article. Please rate it. This will help further readers to get a more realistic picture of it. If you are unclear on anything, do not hesitate to ask your questions on the blog. Live With Passion!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.