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 the string: expr substr the end position of the content starting position, for example: $con = ' expr substr $content ' "1" $ind
----------------------------------------------
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 Ten +
-
$expr +
2500
$expr 30/3
Ten
$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 * 3
-
Numerical 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 text)
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,
then test the last command state, if it is 0, prove that it is a number, and others 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 state, unfortunately the return value is exactly the opposite of the system's last Exit command,
the work returns 1, and any other value is invalid or error. 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.
"Hello" is indeed equal to "hello".
Pattern Matching
expr also has a pattern-matching function. You can use expr to calculate the number of characters in a string by specifying the colon option.
The character repeats 0 or more times.
$value =accounts.doc
$expr $value: '. * '
A
string matching operations can be used in expr, where patterns are used. d o c extracts file name.
$expr $value: ' (. *). doc '
accounts
Array in Shell
with
$varname [0]=value1
$varname [1]=value2
....
to define
with
$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
-----------------------------------------------
The expr command is a manual command-line counter that is used to evaluate the value of an expression variable under Unix/linux, which is generally used for integer values or for strings.
– The format is:
Expr expression (the command reads in the Expression parameter, evaluates its value, and writes the result to standard output)
– Parameter application rules:
separate each item with a space;
precede the shell-specific characters with \ (backslash);
enclose strings that contain spaces and other special characters in quotation marks .
–expr usage examples explain:
(1), calculate the string length
[[email protected] bkeep]# expr Length "bkeep zbb"//Including spaces
9
(2), grab string
[[email protected] bkeep]# expr substr "Bkeep zbb" 4 9
EP ZBB
(3), grab the position where the first character number string appears
[[email protected] bkeep]# Expr index "bkeep zbb" e
3
(4), Integer arithmetic
[[email protected] bkeep]# Expr 9
5
[[email protected] bkeep]# expr 30/3/2//operator must have a space between the numbers
5
(5), increment count
Description: Expr is used in the loop for incremental calculations. The variable is initialized to 0, then the loop value is 1, and the use of the anti-quote is the command substitution.
> Loop=0
> loop= ' expr $LOOP + 1 '
(6), numerical test
Description: Test a number with expr. If an attempt is made to calculate a non-integer, an error is returned.
> rr=3.4
> Expr $RR + 1
expr:non-numeric Argument
> Rr=5
> Expr $RR + 1
6
(7), Pattern matching
Description: Expr also has a pattern matching function. 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.
[email protected] bkeep]# expr Bkeep.doc: '. * '
9
(8) A string match operation can be used in expr, where the schema is used to extract the. doc file as a subordinate name.
[[email protected] bkeep]# expr bkeep.doc: ' \ (. *\). Doc '
bkeep
Expr in the Linux shell command