Use of expr in the Shell

Source: Internet
Author: User

The 1.expr command is generally used for integer values in the general format:
Expr argument operator argument
The general usage is to do arithmetic operations using expr, such as:
[[email protected] ~]# Expr 10 + 20
30
$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. Such as:
[[email protected] ~]# Expr 10 * 10//: Error,
Expr:syntax Error
[[email protected] ~]# Expr 10 \* 10//correct
100
2. Using expr, the output is imported into the/dev/null and can be judged.
If the expression succeeds, it can be represented as follows:
$value =12
$expr $value + >/dev/null 2>&1
$echo $?
0

Returns a value other than 0 if the expression fails
$value =hello
$expr $value + >/dev/null 2>&1
$echo $?
2
3.EXPR also operates for strings
string comparison:
If expr succeeds, it returns a value of 1, and any other value is invalid or error. As the following example tests whether two strings are equal, here the strings are "Hello" and "Hello".
$value =hello
$expr $value = "Hello"
1
# This time the shell returns a value of 0.
$echo $?
0
Pattern Matching for 4.expr:
For expr pattern matching, you can use expr to specify a string pattern match by specifying the colon: option.
He Gofu repeats 0 or more times. The return value is the content in parentheses.
For example, in Tomcat's catalina.sh, the pattern match of expr was used to get the true path of the connection file, as in the following examples:
# Resolve Links-a Softlink
Prg= "$"
While [-H ' $PRG "]; Do
Ls= ' Ls-ld "$PRG" '
link= ' expr ' $ls ': ' .*->.? $ ' # Here the return value of expr is the true path of the connection file
If expr "$link": '/.* ' >/dev/null; Then # path name returns 1
Prg= "$link"
Else
prg= ' dirname ' $PRG '/' $link '
Fi
Done

Use of expr in the Shell

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.