Linux expr command parameters and usage details
Original: http://blog.csdn.net/tianmohust/article/details/7628694
Expr usage
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)
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
17.5.1 Increment Count
Expr is used in the loop for incremental calculations. First, the loop is initialized to 0, and then the loop value is added 1, and the use of the inverted quote means
That is, an alternative command. The most basic is to accept the output from the (expr) command and put it into the loop variable.
$LOOP =0
# $LOOP = ' expr $LOOP + 1 '
17.5.2 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 the reverse of the system's last Exit command, which returns 1 successfully, 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".
17.5.3 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. * meaning that any character repeats 0 or more times.
$value =accounts.doc
$expr $value: '. * '
12
String matching operations can be used in expr, where patterns are used. d o c extracts file name.
$expr $value: ' (. *). Doc '
Accounts
=============================
Do you have an array in the shell, please? Of course, with
$varname [0]=value1
$varname [1]=value2
....
To define the use of
$echo ${varname[0]}
Way to reference
# # # String length
shell>> expr Length "This is a test"
14
# # # Digital Quotient
Shell>> Expr 14% 9
5
# # # Grab a string from a position
Shell>> expr substr "This is a test" 3 5
IS is
# # # Number 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
> Expr Length "This is a test"
14
(2), grab string
> Expr substr "This is a test" 3 5
IS is
(3), grab the position where the first character number string appears
> Expr Index "Sarasara" a
2
(4) True reproduction of strings
> Expr Quote Sara
Sara
(5), Integer arithmetic
> Expr 14% 9
5
> Expr 10 + 10
20
> Expr 1000 + 900
1900
> Expr 30/3/2
5
> Expr 30 \* 3 (when using multiplication sign, you must mask its specific meaning with a backslash.) Because the shell may misunderstand the meaning of the asterisk display)
90
> Expr 30 * 3
Expr:syntax Error
(6), 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 '
(7), 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
(8), 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.
> Value=account.doc
> Expr $VALUE: ‘. *’
8
String matching operations can be used in expr, where the schema is used to extract the. doc file as a subordinate name.
$expr $VALUE: '.?. Doc
Accounts
(ext.) Linux Expr command parameters and usage explanations