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