The expr command is a manual command line counter used to evaluate the value of expression variables in Unix/Linux. It is generally used as an integer or a string.
-Format:
Expr expression (the command reads the expression parameter, computes its value, and writes the result to the standard output)
-Parameter application rules:
Separate each item with spaces;
Place the slash (/) before a specific shell character;
Enclose strings containing spaces and other special characters in quotation marks.
-Expr usage example:
(1) Calculate the string length
> Expr length "this is a test"
14
(2) Capture strings
> Expr substr "this is a test" 3 5
Is is
(3) capture the position where the first character numeric string appears
> Expr Index "sarasara"
2
(4) True string Reproduction
> Expr quote Sara
Sara
(5) integer calculation
> Expr 14% 9
5
> Expr 10 + 10
20
> Expr 1000 + 900
1900
> Expr 30/3/2
5
> Expr 30/* 3 (when using a multiplication number, you must use a backslash to block its specific meaning. Because shell may misunderstand the meaning of the asterisk)
90
> Expr 30*3
Expr: syntax error
(6) incremental count
Note: expr is used for incremental computing in a loop. Initialize the variable to 0 first, and then add the loop value to 1. The reverse quotation marks are used as commands instead.
> Loop = 0
> Loop = 'expr $ loop + 1'
(7) numerical test
Note: Use expr to test a number. If you try to calculate a non-integer value, an error is returned.
> RR = 3.4.
> Expr $ RR + 1
Expr: Non-numeric argument
> RR = 5
> Expr $ RR + 1
6
(8) pattern matching
Note: expr also supports pattern matching. You can use expr to specify the colon option to calculate the number of characters in the string .. * Meaning that any character must be repeated 0 times or multiple times.
> Valueappsaccount.doc
> Expr $ value :'.*'
8
In expr, you can use the wildcard string matching operation. In this example, you can use the wildcard Name Of The. DOC file.
$ Expr $ value: '/(. */developer.doc'
Accounts