Shell Instruction Expr Usage guide

Source: Internet
Author: User
Tags arithmetic array definition numeric numeric value
In the Linux shell command expr is not very noticeable, but it works very well. So far, I personally think the biggest role is two-arithmetic and string operations.
First said arithmetic, in the shell arithmetic not simple subtraction, should write the following format:


where "=" after the "'" wrapped expression, this symbol in the shell is very useful, is the TAB key above "~" the original form. Can be used to save the results of many commands to a variable.  Then there is the operator, the left and right sides of the operator must be spaces, otherwise there will be an error. Then there is the powerful string operation:


Extracts the subscript for the specified character: Expr index content character, for example:



Extract substring of string: expr substr the end position of the content starting position, for example:


The expr command is generally used for integer values, but can also be used for strings. The General format is:

Expr argument operator argument


Expr is also a manual command-line counter.

$expr + 
$expr 1500 + 900 
2500 
$expr 30/3 
$expr 30/3/2 
(Note that there are spaces around the operator, if there is no space to indicate a string connection)
When using multiplication, you must use a backslash to mask its specific meaning. Because the shell may misunderstand the meaning of displaying the asterisk.
$EXPR * 3 
90 


Numerical test


You can test a number with expr. If you attempt to compute a non-integer, an error is returned.

$RR =1.1 
$expr $rr + 1 
expr:non-numeric argument 
$rr =2 
$expr $RR + 1 
(Note: This example differs from the original)
Here you need to assign a value to a variable (regardless of its content), perform a numeric operation, and import the output into Dev/null.
The final command state is then tested, if 0, to prove that it is a number, and others to indicate a non-numeric value.
$value =12 
$expr $value + >/dev/null 2>&1 
$echo $? 

This is a number.


$value =hello 
$expr $value + >/dev/null 2>&1 
$echo $? 
This is a non-numeric character.


Expr can also return its own exit state, unfortunately the return value is exactly the opposite of the system's last Exit command, successfully returning 1, any other value being invalid or incorrect. The following example tests whether two strings are equal, where the string is
"Hello" and "Hello".

$value =hello 
$expr $value = "Hello" 
1 
$echo $? 
0

Expr returns 1. Don't confuse it, it shows success. Now verify its last exit status, return 0 to indicate that the test was successful,
"Hello" really equals "hello".


Pattern matching


Expr also has pattern matching capabilities. You can use expr to calculate the number of characters in a string by specifying the colon option ... * meaning that any character is repeated 0 or more times.

$value =accounts.doc 
$expr $value: '. * ' 


You can use string matching operations in expr, where you use patterns. D o C Extract the file's subordinate name.

$expr $value: ' (. *). Doc ' 


Array definition in shell

$varname [0]=value1 
$varname [1]=value2 

Reference

To grab a string from a location
Shell>> expr substr "This is a test" 3 5 

Number string only the the ' the ' character
shell>> Expr Index "Testforthegame" E 

String true Recurrence
shell>> Expr Quote Thisisatestformela 

Related Article

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.