Shell learning notes-conditional attention

Source: Internet
Author: User

Shell learning notes-conditional attention
1. format 1) test parameter file example: test-e/root/install. log 2) [parameter file] -- recommended example: [-e/root/install. log] Note: There must be spaces at the end of the brackets and at the front. file type parameter 1)-d file: determines whether the file exists and is a directory file 2)-e file: determines whether the file exists 3)-f file: determine whether a file exists and whether it is a common file 4)-s file: Determine whether the file exists and whether it is not empty. 5) other file types:-B device files; -c character device file;-L symbol link file;-p pipeline file;-S socket file example: [root @ localhost ~] # [-D/root] & echo yes | echo noyes [root @ localhost ~] # [-E/root/install. log] & echo yes | echo noyes [root @ localhost ~] # [-F/root/install. log] & echo yes | echo noyes [root @ localhost ~] # [-S/root/install. log] & echo yes | echo noyes 3. file Permission parameter 1)-r file: determines whether the file exists and has read permission 2)-w file: determines whether the file exists and whether the file has write permission 3) -x file: determines whether the file exists and whether the file has the execution permission. 4) other file permissions:-u SUID permission;-g SGID permission;-k SBit permission example: [root @ localhost ~] # [-R/root/install. log] & echo yes | echo noyes [root @ localhost ~] # [-W/root/install. log] & echo yes | echo noyes [root @ localhost ~] # [-X/root/install. log] & echo yes | echo nono 4. compare two files 1) file 1-nt file 2: Determine whether the modification time of file 1 is later than that of file 2 2 2) file 1-ot file 2: determine whether the modification of file 1 is earlier than that of file 2. 3) file 1-ef file 2: Determine whether file 1 is consistent with the lnode number of file 2, it can be understood that two files are the same file. This judgment is a good method for judging hard links. Example: [root @ localhost ~] # [/Root/install. log-nt/root/install. log. syslog] & echo yes | echo noyes [root @ localhost ~] # [/Root/install. log-ot/root/install. log. syslog] & echo yes | echo nono [root @ localhost ~] # [/Root/install. log-ef/root/install. log. syslog] & echo yes | echo nono 5. two integers: 1) integer 1-eq integer 2: judge whether it is equal 2) integer 1-ne integer 2: judge whether it is not equal 3) integer 1-gt integer 2: judge whether it is greater than 4) integer 1-lt integer 2: judge whether it is less than 5) integer 1-ge integer 2: judge whether it is greater than or equal to 6) integer 1-le integer 2: judge whether it is less than or equal. Note: In shell, all variables are character types, but when an integer comparison parameter is added, the variables on both sides are considered as integer types. Example: [root @ localhost ~] # [1-eq 1] & echo yes | echo noyes [root @ localhost ~] # [1-ne 1] & echo yes | echo nono [root @ localhost ~] # [2-gt 1] & echo yes | echo noyes [root @ localhost ~] # [2-lt 1] & echo yes | echo nono [root @ localhost ~] # [1-ge 1] & echo yes | echo noyes [root @ localhost ~] # [1-le 1] & echo yes | echo noyes 6. string judgment 1)-z string: judge whether the string is null 2)-n string: Determine whether the string is not null 3) string 1 = string 2: Determine whether two strings are equal 4) string 1! = String 2: Example of determining whether two strings are not equal: [root @ localhost ~] # Str = "abc" [root @ localhost ~] # [-Z $ str] & echo yes | echo nono [root @ localhost ~] # [-N $ str] & echo yes | echo noyes [root @ localhost ~] # Str2 = "efg" [root @ localhost ~] # ["$ Str" = "$ str2"] & echo yes | echo nono [root @ localhost ~] # ["$ Str "! = "$ Str2"] & echo yes | echo noyes 7. multi-condition judgment 1) judgment 1-a judgment 2: represents logic and, equivalent to and 2) judgment 1-o judgment 2: represents logic or, equivalent to or 3 )! Judgment: indicates the logic is not, which is equivalent to the not example: [root @ localhost ~] # [1-eq 1-a 1-eq 2] & echo yes | echo nono [root @ localhost ~] # [1-eq 1-o 1-eq 2] & echo yes | echo noyes [root @ localhost ~] #[! 1-eq 2] & echo yes | echo noyes

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.