Expr command usage-instance description

Source: Internet
Author: User

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

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.