Chapter 6 of linuxshell programming guide ------ conditional testing

Source: Internet
Author: User
When writing a script, you may need to check whether the string is equal or not. you may also need to check the file status or perform a digital test. Further actions can be performed based on these tests. The Test command is used to Test strings, file statuses, and numbers. it is also suitable for the next chapter to determine whether the strings are equal when writing scripts such as if, then, and els, you may also need to check the file status or perform a digital test. Based on this
To perform further operations. The Te s t command is used to test strings, file statuses, and numbers.
The I f, t h e n, e l s e condition structures mentioned.
This chapter includes:
? Use the t e s t command for files, strings, and numbers.
? Use the e x p r command for numbers and strings.
E x p r command to test and execute numerical output. Run the exit command $? We can test t e s t and e x p r, both of which are in table 0.

The description is correct. 1 indicates that an error is returned.

E s t generally has two formats:
Test condition
Or
[C o n d I t I o n]
When square brackets are used, add spaces on both sides of the condition.
There are many conditional expressions for the test file status, but the most common expressions can be found in table 1 7-1.

Table 17-1 file status test
-D Directory-s file length is greater than 0, not empty
-F: regular files-w: writable
-L symbolic connection-u file has s u I d bit settings
-R readable-x executable

Use two methods to test whether the file s c o r e s. t x t can be written and use the final exit status to test whether the file is successful. Remember, 0 indicates
Successful. Otherwise, the request fails.

[Root @ localhost huangcd] # ls-l OK .txt
-Rw-r -- 1 root 30 12-03 OK .txt
[Root @ localhost huangcd] # [-w OK .txt]
[Root @ localhost huangcd] # echo $?
0

[Root @ localhost huangcd] # [-x OK .txt]
[Root @ localhost huangcd] # echo $?
1 // not executable

The following example tests whether a p s B I n directory exists:
[Root @ localhost huangcd] # [-d release Bin]
[Root @ localhost huangcd] # echo $?
1

Whether the s u I d bit is set for the test file permission:

[Root @ localhost huangcd] # [-u OK .txt]
[Root @ localhost huangcd] # echo $?
1

Test whether the file status is o k, but sometimes two file statuses must be compared. S h e l provides three logical operations to complete this
Function.
-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.

The following example tests whether two files can be read.

[Root @ localhost huangcd] # [-r OK .txt-a-r sed. out]
[Root @ localhost huangcd] # echo $?
0

String testing is an important part of error capture, especially when testing user input or compare variables.
There are five formats for string testing.

Test "string"

Test str_op "string"

Test "string" str_op "string"

[Str_op string]

[String str_op string]

Here, s t r I n g o p e r a t o r can be:
= Two strings are equal.
! = Two strings.
-Z empty string.
-N is a non-empty string.

TO test whether the environment variable e d I TO R is null:

[Root @ localhost huangcd] # [-z $ EDITOR]
[Root @ localhost huangcd] # echo $?
0 // it is null

Is the value v I?

[Root @ localhost huangcd] # [$ EDITOR = "vi"]
[Root @ localhost huangcd] # echo $?
0

Test whether the TAPE box TAPE2 is equal.

[Root @ localhost huangcd] # TAPE = "/dev/rmt0"
[Root @ localhost huangcd] # TAPE2 = "/dev/rmt1"

[Root @ localhost huangcd] # ["$ TAPE" = "$ TAPE2"]
[Root @ localhost huangcd] # echo $?
1 // not equal

Many operators can be used to test the value. The general format is as follows:
"N u m B e r" n u m e r I c o p e r a t o r "n u m B e r"
Or
["N u m B e r" n u m e r I c o p e r a t o r "n u m B e r"]
N u m e r I c o p e r a t o r can be:
-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 huangcd] # number = 130
[Root @ localhost huangcd] # "$ number"-eq "100"
Bash: 130: command not found
[Root @ localhost huangcd] # ["$ number"-eq "130"]
[Root @ localhost huangcd] # echo $?
0
[Root @ localhost huangcd] # ["$ number"-gt "100"] // whether the value is greater
[Root @ localhost huangcd] # echo $?
0

The e x p r command is generally used for integer values, but can also be used for strings. The general format is:
Expr argument operator argument
E x p r is also a manual command line counter.

[Root @ localhost huangcd] # expr 10 + 10
10 + 10
[Root @ localhost huangcd] # expr 10 + 10
20
[Root @ localhost huangcd] # expr 30/3
10
When using a multiplication number, you must use a backslash to block its specific meaning. Because s h e l may misunderstand the meaning of the asterisk.

[Root @ localhost huangcd] # expr 30 \ * 3
90

E x p r 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 (e x p r) command and put it into the cyclic variable.

[Root @ localhost huangcd] # LOOP = 0
[Root @ localhost huangcd] # LOOP = 'expr $ LOOP + 1'
[Root @ localhost huangcd] # echo LOOP
LOOP
[Root @ localhost huangcd] # echo $ LOOP
1
[Root @ localhost huangcd] # LOOP = 'expr $ LOOP + 1'
[Root @ localhost huangcd] # echo $ LOOP
2

E x p r 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
"H e l o" and "h e l o ".

This chapter describes the basic functions of e x p r and t e s t, describes how to test the file status and assign values to strings.
Conditional expressions such as if then else and c a s e can be used for a wider range of tests and some actions on the test results.

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.