Linux expr Usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.