Learn Shell-shell the usage of expr

Source: Internet
Author: User
Tags numeric value

Original link: http://desheng666.blog.163.com/blog/static/4908408220121643953425/

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 10 + 10
20
$EXPR 1500 + 900
2500
$expr 30/3
10
$expr 30/3/2
5
(Note that operators have spaces around them, and if no spaces indicate string connections)
When using multiplication sign, you must mask its specific meaning with a backslash. Because the shell may misunderstand the meaning of displaying asterisks.
$EXPR 30 * 3
90

  Numeric test
You can test a number with expr. If you attempt to calculate a non-integer, an error is returned.
$rr =1.1
$expr $rr + 1
expr:non-numeric argument
$rr =2
$expr $rr + 1
3
(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 to Dev/null,
and then test the final command state, if 0, to prove that this is a number, and others to indicate a non-numeric value.
$value =12
$expr $value + >/dev/null 2>&1
$echo $?
0
This is a number.
$value =hello
$expr $value + >/dev/null 2>&1
$echo $?
2
This is a non-numeric character.

Expr can also return its own exit status, but unfortunately the return value is exactly the opposite of the system's last Exit command, which returns 1 as
work, and any other value is 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 check its final exit status and return 0 to indicate that the test was successful and
"Hello" is indeed equal to "hello".

  pattern matching
Expr also has pattern matching functionality. 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 =accounts.doc
$expr $value: '. * '


You can use string matching operations in expr, where patterns are used. D o c extracts the file name.
$expr $value: ' (. *). doc '
accounts

Array in shell

Use
$varname [0]=value1
$varname [1]=value2
....
To define
Use
$echo ${varname[0]}
Way to reference

grab a string from a location
  
Shell>> expr substr "This is a test" 3 5
IS is
  
Digital String only the first character
  
shell>> Expr Index "Testforthegame" E
2
  
True reproduction of strings

  
shell>> Expr Quote Thisisatestformela
Thisisatestformela

Share to:
    • /////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////
    • Although expr is not very noticeable in the Linux shell command, it is very large in function! So far, I personally see the biggest role is two--arithmetic and string manipulation.

      First say arithmetic, in the shell arithmetic not simple subtraction, should write the following format:

      $val 1= ' $val 2-1 '

      where "=" is followed by "'" to wrap the expression, this symbol in the shell is very useful, is the TAB key above the original form of "~". Can be used to save the results of many commands into a variable. Then the operator, the left and right sides of the operator must be a space, or an error occurs.


      Followed by a powerful string operation:

      Extracts the subscript of the specified character: Expr index content character, for example: $ind = ' expr index ' $content "" = "

      Extract substring of string: expr substr the end position of the content starting position, for example: $con = ' expr substr $content ' "1" $ind '

Learn Shell-shell the use of expr (turn)

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.