The expr command is a manual command-line counter that is used to evaluate the value of an expression variable under Unix/linux, which is generally used for integer values or for strings.
– The format is:
Expr expression (the command reads in the Expression parameter, evaluates its value, and writes the result to standard output)
– Parameter Application rules:
Separate each item with a space;
Precede the shell-specific characters with \ (backslash);
Enclose strings that contain spaces and other special characters in quotation marks.
–EXPR usage Examples explain:
(1), calculate the string length
> Expr Length "This is a test"
14
(2), grab string
> Expr substr "This is a test" 3 5
IS is
(3), grab the position where the first character number string appears
> Expr Index "Sarasara" a
2
(4) True reproduction of strings
> Expr Quote Sara
Sara
(5), Integer arithmetic
> Expr 14% 9
5
> Expr 10 + 10
20
> Expr 1000 + 900
1900
> Expr 30/3/2
5
> Expr 30 \* 3 (when using multiplication sign, you must mask its specific meaning with a backslash.) Because the shell may misunderstand the meaning of the asterisk display)
90
> Expr 30 * 3
Expr:syntax Error
(6), increment count
Description: Expr is used in the loop for incremental calculations. The variable is initialized to 0, then the loop value is 1, and the use of the anti-quote is the command substitution.
> Loop=0
> loop= ' expr $LOOP + 1 '
(7), numerical test
Description: Test a number with expr. If an attempt is made to calculate a non-integer, an error is returned.
> rr=3.4
> Expr $RR + 1
Expr:non-numeric argument
> rr=5
> Expr $RR + 1
6
(8), pattern matching
Description: Expr also has a pattern matching function. You can use expr to calculate the number of characters in a string by specifying the colon option. * meaning that any character repeats 0 or more times.
> Value=account.doc
> Expr $VALUE: '. * '
12
String matching operations can be used in expr, where the schema is used to extract the. doc file as a subordinate name.
$expr $VALUE: '. ?. Doc
$expr $VALUE: ' (. *). Doc '
This example is matching the rectification file name, looking at the
Transferred from: http://blog.csdn.net/cbk861110/article/details/18730099
Linux expr Usage