Shell learning notes and shell scripts

Source: Internet
Author: User

Shell learning notes and shell scripts
1. determine the 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.

2. Determine file type parameters

1)-d file: determines whether the file exists and is a directory file

2)-e file: determines whether the file exists

3)-f file: determines whether the file exists and is a common file

4)-s file: determines whether the file exists and is not empty

5) other file types:

-Block B device files;-c character device files;-L symbol link files;-p pipe files;-S socket files

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. Determine File Permission Parameters

1)-r file: determines whether the file exists and has read permission

2)-w file: determines whether the file exists and whether it has the write permission

3)-x file: determines whether the file exists and has the execution permission.

4) Other File Permission judgment:

-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. Comparison of Two files

1) file 1-nt file 2: Determine whether the modification time of file 1 is newer than that of file 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. Comparison of two integers

1) integer 1-eq integer 2: judge whether it is equal

2) integer 1-ne integer 2: determines if it is not equal

3) integer 1-gt integer 2: determines whether the value is greater

4) integer 1-lt integer 2: judge whether it is smaller

5) integer 1-ge integer 2: determines whether the value is greater than or equal

6) integer 1-le integer 2: judge whether it is less than or equal

Note: In shell, all variables are of the character type, but when an integer comparison parameter is added, the variables on both sides are considered to be of the integer type.

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: determines whether the string is null

2)-n string: determines whether the string is not empty

3) string 1 = string 2: judge whether two strings are equal

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

Example:

[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 the logic and, equivalent to and

2) 1-o judgment 2: logical or, equivalent to or

3 )! Judgment: indicates that the logic is not, which is equivalent to 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.