Linux shell expr use collectors: Spring and Autumn hundred flavor the-Geneva- +|read: Go:| |share non-original, excerpt from: "Linux and Unix Shell Programming Guide"17.5expr Usage The expr command is generally used for integer values, but can also be used with strings. The general format is: expr argumentoperatorargumentexpr is also a manual command line counter. # exprTen+Ten -# expr the+ the2500# expr -/3Ten# expr -/3/25(note that operators have spaces around them) when using multiplication sign, you must use a backslash to mask its specific meaning. Because the shell may misunderstand the meaning of displaying asterisks. # expr -*3 -17.5.1increment 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 anti-quote means the substitution 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.2A numeric test can test a number with expr. If you attempt to calculate a non-integer, an error is returned. # RR=1.1# expr $RR+1Expr:non-Numeric ARGUMENT$RR=2# expr $RR+13(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, and then test the last command state, if it is 0, prove that this is a number, and the others indicate a non-numeric value. # value= A# expr $value+Ten>/dev/NULL 2>&1# echo $?0this is a number. # value=hello# expr $value+Ten>/dev/NULL 2>&1# echo $?2This 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 $?0expr 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 that "hello" does equal "Hello". 17.5.3pattern 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:'.*' Astring matching operations can be used in expr, where patterns are used. d o c extracts file name. # expr $value:'(. *). doc'accounts
Linux Shell expr uses