Centos command -- exprtest

Source: Internet
Author: User
Use the test command for files, strings, and numbers. Use the expr command for numbers and strings. Expr command to test and execute numerical output. Run the exit command $? Test and expr can be tested, both of which indicate 0 is correct, and 1 indicates that an error is returned. Expr usage the expr command is generally used for integer values, but can also be used for strings. The general format is: exprargumentoperatorargumentexpr is also a manual command line count.

Use the test command for files, strings, and numbers.
Use the expr command for numbers and strings.
Expr command to test and execute numerical output. Run the exit command $? It can be tested that tes t and expr are correct in 0 and return error in 1.

Expr usage
The expr command is generally used as an integer, but can also be used as a string. 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 there are spaces around the operator)
When using a multiplication number, you must use a backslash to block its specific meaning. Because shell may misunderstand the meaning of the asterisk.
# Expr 30*3
(Correct operation)
# Expr 5 \ * 4

Incremental count
Expr is used for Incremental computing in a loop. First, the Loop Initialization is 0, and then the loop value is added with 1. the usage of the anti-quotation marks
That is, the replacement command. The most basic one is to accept the output from the (expr) command and put it into the cyclic variable.
# LOOP = 0
# LOOP = 'expr $ LOOP + 1'

Numerical test
You can use expr to test a number. If you try to calculate a non-integer value, an error is returned.
# Rr = 1.1.
# Expr $ rr + 1
Expr: non-numeric argument
$ Rr = 2
# Expr $ rr + 1
3
Here, you need to assign a value to the variable (regardless of its content) for numerical calculation, and import the output to dev/null,
Then test the final command status. if it is 0, it indicates that this is a number. if it is other words, it indicates that it is not a numerical value.
# Value = 12
# Expr $ value + 10>/dev/null 2> & 1
# Echo $?
0
This is a number.
# Value = hello
# Expr $ value + 10>/dev/null 2> & 1
# Echo $?
2
This is a non-numeric character.

Expr can also return its exit status. Unfortunately, the return value is the opposite of the system exit command.
1 is returned, and any other value is invalid or incorrect. The following example tests whether two strings are equal. here the string is
"Hello" and "hello ".
# Value = hello
# Expr $ value = "hello"
1
# Echo $?
0
Expr returns 1. Don't confuse it. this indicates success. Check the final exit status. if 0 is returned, the test is successful,
"Hello" is indeed equal to "hello ".

Test usage

Test generally has two formats: test condition or [condition]. When square brackets are used, add spaces on both sides of the condition.

Common file status tests:
-D Directory
-S file length is greater than 0, not empty
-F: regular file
-W writable
-L symbolic connection
-The u file has suid settings
-R readable
-X executable
-C character device file

Use two methods to test whether the scores.txt file can be written and use the final exit state to test whether the file is successful. Remember: 0 indicates success, and others indicate failure.

[Root @ localhost ~] # Ls-l
-Rw-r -- 1 root 5 Sep 21 18:43 scores.txt

[Root @ localhost ~] # [-W scores.txt]
[Root @ localhost ruby] # echo $?
0

[Root @ localhost ~] # Test-w scores.txt
[Root @ localhost ~] # Echo $?
0

Note: whether to read, write, and execute the file depends on the permissions of the current user on the file. If we change to another user, the results are not necessarily the same.

Use logical operators during testing
-Logic a and the operators are true on both sides, and the result is true. Otherwise, the result is false.
-O logic or, if either side of the operator is true, the result is true. Otherwise, the result is false.
! Logical no. the condition is false and the result is true.

[Root @ localhost ~] # Ls-l
-Rw-r -- 1 root 0 Sep 21 19:39
-Rw-r -- 1 root 0 Sep 21 19:39 B

[Root @ localhost ~] # [-W a-x B]
[Root @ localhost ~] # Echo $?
1
[Root @ localhost ~] # [-W a-o-x B]
[Root @ localhost ~] # Echo $?
0
[Root @ localhost ~] #[! -W a-o-x B]
[Root @ localhost ~] # Echo $?
1

String comparison
= Two strings are equal.
! = Two strings.
-Z empty string.
-N is a non-empty string.

[Root @ localhost ~] # A = ""
[Root @ localhost ~] # [-Z $ a]
[Root @ localhost ruby] # echo $?
0

[Root @ localhost ~] # A = "123"
[Root @ localhost ~] # [-Z $ a]
[Root @ localhost ~] # Echo $?
1
[Root @ localhost ~] # [-N $ a]
[Root @ localhost ~] # Echo $?
0

Test value
-The eq value is equal.
-The ne values are not equal.
-The first number of gt is greater than the second number.
-Lt: The first number is smaller than the second number.
-The first number of le is less than or equal to the second number.
-The first number of ge is greater than or equal to the second number.

[Root @ localhost ~] # ["123"-eq "123"-a "123"-ge "100"]
[Root @ localhost ~] # Echo $?
0
[Root @ localhost ~] # A = 12
[Root @ localhost ~] # B = 10
[Root @ localhost ~] # [$ A-gt $ B]
[Root @ localhost ~] # Echo $?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.