Home php教程 PHP开发 The use of expr in Shell

The use of expr in Shell

Nov 19, 2016 am 11:21 AM
shell

1.expr command is generally used for integer values. Its general format is:
expr argument operator argument
The general usage is to use expr to do arithmetic operations, such as:
[root@centos ~]# expr 10 + 20
30
$ expr 30 / 3 / 2
5
(Note that there are spaces around the operator, if there are no spaces, it means string concatenation)
When using the multiplication sign, its specific meaning must be masked with a backslash. Because the shell may misunderstand the meaning of the asterisk. For example:
[root@centos ~]# expr 10 * 10 //: Error,
expr: syntax error
[root@centos ~]# expr 10 * 10 //Correct
100
2. Use expr to import the output /dev/null can be used for judgment.
If the expression is successful, it can be expressed as follows:
$value=12
$expr $value + 10 > /dev/null 2>&1
$echo $?
0

If the expression fails, a non-zero value is returned
$value=hello
$expr $value + 10 > /dev/null 2>&1
$echo $?
2
3.expr also operates on strings
String comparison:
expr If successful, return value 1 , any other value is invalid or an error. For example, the following example tests whether two strings are equal, where the strings are "hello" and hello".
$value=hello
$expr $value = "hello"
1
#The return value of the shell at this time is 0.
$echo $?
0
4.expr pattern matching:
Regarding expr pattern matching, you can use expr to specify the string pattern matching by specifying the colon: option. .* represents any
character repeated 0 or more times. . The return value is the content in parentheses. For example, in tomcat's catalina.sh, expr pattern matching is used to obtain the real path of the connection file. The example is as follows:
# resolve links - $0 may be a softlink
PRG="$0 "
while [ -h "$PRG" ]; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> .*$'` # expr here The return value is the real path of the connection file
if expr "$link" : '/.*' > /dev/null; then #The path name returns 1
PRG="$link"
else
PRG=`dirname "$ PRG"`/"$link"
fi
done

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install Classic Shell on Windows 11? How to install Classic Shell on Windows 11? Apr 21, 2023 pm 09:13 PM

How to install Classic Shell on Windows 11?

Explorer.exe does not start on system startup [Fix] Explorer.exe does not start on system startup [Fix] Jun 03, 2023 am 08:31 AM

Explorer.exe does not start on system startup [Fix]

PowerShell deployment fails with HRESULT 0x80073D02 issue fixed PowerShell deployment fails with HRESULT 0x80073D02 issue fixed May 10, 2023 am 11:02 AM

PowerShell deployment fails with HRESULT 0x80073D02 issue fixed

How to quickly delete the line at the end of a file in Linux How to quickly delete the line at the end of a file in Linux Mar 01, 2024 pm 09:36 PM

How to quickly delete the line at the end of a file in Linux

Different ways to run shell script files on Windows Different ways to run shell script files on Windows Apr 13, 2023 am 11:58 AM

Different ways to run shell script files on Windows

Super hardcore! 11 very practical Python and Shell script examples! Super hardcore! 11 very practical Python and Shell script examples! Apr 12, 2023 pm 01:52 PM

Super hardcore! 11 very practical Python and Shell script examples!

Here are the fixes for Open Shell Windows 11 not working issue Here are the fixes for Open Shell Windows 11 not working issue Apr 14, 2023 pm 02:07 PM

Here are the fixes for Open Shell Windows 11 not working issue

How to install Open Shell to restore the classic Start menu on Windows 11 How to install Open Shell to restore the classic Start menu on Windows 11 Apr 18, 2023 pm 10:10 PM

How to install Open Shell to restore the classic Start menu on Windows 11

See all articles