Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

There are two things to note in this set of operations.


    When    you multiply,   you must    use an  escape  character   before  the
multiplication operator for the command to parse correctly, which is why
you see \ (the escape character) before the * (the multiplication
character).
When you divide, only the whole number result will be returned, so, for
example, expr 11 / 3 returns 3 . To get the remainder, use the
following, which returns 2 :
expr 11 / 3

There are a number of string-related operations available. Each involves
adding a second word to the command before the string to be evaluated.


To find the length of a string, use the following:


Click here to view code image
expr length string


If the string includes spaces, you must again use the escape character, like
this:


Click here to view code image
expr length linux\ is\ cool
13


There are many more things you can do with expr. Read the man page for
more information.


The case Statement


You use the case statement to execute statements depending on a discrete
value or a range of values matching the specified variable. In most cases, you
can use a case statement instead of an if statement if you have a large
number of conditions.


The format of a case statement for pdksh and bash is as follows:


Click here to view code image
case str in
str1 | str2)
Statements;;
str3|str4)
Statements;;
*)
Statements;;

Free download pdf