Shell Learning notes-conditional judgment

Source: Internet
Author: User

1. Judging the format

1) Test parameter file

Example: Test-e/root/install.log

2) [parameter file]--Recommended use

Example: [-e/root/install.log]

Note: Spaces are required after and before the brackets

2. determine file type parameters

1)-D file: Determine if the file exists and whether it is a directory file

2)-e file: Determine if the file exists

3)-F file: Determine if the file exists and is a normal file

4)-S file: Determine if the file exists and is not empty

5) Other file type judgment:

-B block device file;-c character device file;-l symbolic link file;-p pipe file;-S socket file

Example:

Echo Echo  ~]# [-e/root/installechoecho~]# [-f/root/installEcho  Echo~]# [-s/root/installechoecho  Noyes

3. Determine file permission parameters

1)-R file: Determines if the file exists and has Read permission

2)-W file: Determine if the file exists and write permission

3)-X file: Determines if the file exists and has execute permissions

4) Other file permissions to judge:

-u suid permissions;-g sgid permissions;-k sbit permissions

Example:

[[Email protected] ~]# [-r/root/installechoecho~]# [-w /root/  Installechoecho ~]# [-x/root/installechoecho Nono

4. Comparison of two files

1) file 1-nt file 2: Determine if file 1 modification time is newer than file 2

2) file 1-ot file 2: Determine if the modification of file 1 is older than file 2

3) file 1-ef File 2: Determine whether the file 1 is the same as the lnode of file 2, it can be understood that two files are the same file. This judgment is a good way to judge hard links.

Example:

[Email protected] ~]# [/root/install. log-nt/root/Installechoecho~]# [/root/Installlog-ot/root/installechoecho~]# [/root/Install . log-ef/root/Installechoecho  Nono

5. Comparison of two integers

1) Integer 1-eq integer 2: determine equality

2) Integer 1-ne integer 2: Determines whether inequality is not equal

3) Integer 1-GT integer 2: Determine if it is greater than

4) Integer 1-LT integer 2: Determines whether it is less than

5) Integer 1-ge integer 2: Determines whether it is greater than or equal to

6) Integer 1-le integer 2: Determines whether it is less than or equal to

Note: In the shell, all variables are character types, but an integer comparison parameter is added, and the variables on both sides are considered integer types.

Example:

[Email protected] ~]# [1-eq1] &&EchoYes | |EchoNoyes[[email protected]~]# [1-ne1] &&EchoYes | |EchoNono[[email protected]~]# [2-gt1] &&EchoYes | |EchoNoyes[[email protected]~]# [2-lt1] &&EchoYes | |EchoNono[[email protected]~]# [1-ge1] &&EchoYes | |EchoNoyes[[email protected]~]# [1-le1] &&EchoYes | |EchoNoyes

6. String Judgments

1)-Z string: Determine if the string is empty

2)-N string: Determines whether the string is not empty

3) String 1 = = String 2: Determine if two strings are equal

4) String 1! = String 2: Determines whether two strings are not equal

Example:

[Email protected] ~]# str="ABC"[[Email protected]~]# [-Z $str] &&EchoYes | |EchoNono[[email protected]~]# [-N $str] &&EchoYes | |EchoNoyes[[email protected]~]# str2="EFG"[[Email protected]~]# ["$str"=="$str 2"] &&EchoYes | |EchoNono[[email protected]~]# ["$str"!="$str 2"] &&EchoYes | |EchoNoyes

7. Multiple conditional judgments

1) Judge 1-a Judgment 2: The expression of logic and, equivalent to and

2) Judging 1-o Judgment 2: Representation of logic or, equivalent to or

3)! Judgment: Indicates logical non, equivalent to not

Example:

1 1 1 2  Echo Echo  1112  echoecho1  2  echoecho  Noyes

Shell Learning notes-conditional judgment

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.