Shell Learning condition judgment test

Source: Internet
Author: User

1.Follow"File Type"to Judge(presence or not)

- e:does the "file" exist? exist as true(Common)
-F:is the "file" aNormalfile(file)? (Common)
- D:whether the "file" is a directoryfile(directory)? (Common)
- b:whether the "file" is ablock device files (Block Devic)? Device
- C:whether the "file" is acharacter device files (character Device)? Device

-s: Change"File"Is it non-empty?
- S:whether the "file" is aSocket File(Socket)? file
- P:whether the "file" is aPipeline Files FIFO (pipe) ? file
- L:whether the "file" is aSymbolic Link File? file 

Two kinds of judging formats

Case :

[[Email protected]/]# CD mnt/[[email protected] mnt]# ls abc.txt[[email protected]/]# test-e/mnt/abc.txt/* Verify that the file exists * /[[email protected]/]# echo $? /* Verify that the previous command is output correctly. Correct 0, error 1 */0[[email protected]/]# [-E/MNT/ABC]/* note. Space between brackets and the command character */[[email protected]/]# echo $?1
But echo $? More trouble, you can use && | | Symbol to determine if the command output is correct. as follows: [[email protected]/]# [-e/mnt/abc.txt] && echo "Yes" | | echo "No" yes


" file permissions ( )




-u: Whether the file name has Permission to "SUID"?


Example: Check file writable executable permission (as long as the other person in the genus group is the correct output)

[[email protected] mnt]# ls-l total usage 0-rw-r--r--. 1 root root 0 October 01:55 123.txt-rw-r--r--. 1 root root 0 October 23:39 abc.txt[[email protected] mnt]# [[email protected] mnt]# [-x Abc.txt] && echo "Yes" | | echo "No" no[[email protected] mnt]# [w abc.txt] && echo "Yes" | | echo "No" yes


3. Comparison between two files
-nt (newer than) to determine whether File1 is newer than file2
-ot (older than) to determine whether File1 is older than file2
-ef judge whether File2 and File2 are the same file, can be used in judging hard link judgment. The main significance in determining whether two files are pointing to the same inode number!

Example: Determine if file Abc.txt is newer than file 123.txt

[[email protected] mnt]# ls-l total usage 0-rw-r--r--. 1 root root 0 October 01:55 123.txt-rw-r--r--. 1 root root 0 October 23:39 abc.txt[[email protected] mnt]# [abc.txt-nt 123.txt] && echo "Yes" | | echo "No" no







-ge n1 greater than or equal to N2 (greater than or equal) -le N1 is less than or equal to N2 (lesser than or equal)


Example:/*22 is equal to 23, is output Yes, not output no*/

[[Email protected]/]# [22-eq] && echo "Yes" | | echo "No" no





note: -n can also omit
character 1 = = character 2 Determines whether the character 1 equals the character 2 and, if equal, the return true
character 1! = Character 2 to determine if character 1 is not equal to character 2, and if equal, the callback is false


Example: Judging whether the variable AA and the variable bb are equal

[[email protected]/]# Aa=qaz/* Assignment variable */[[email protected]/]# bb=qaz[[email protected]/]# [$AA = = $bb] && echo " Yes "| | echo "No" yes


6. Multiple condition determination,

The-A (and) is both in the same condition and returns to true.

-O (or) if any one of the two states is established, return to true.

! logical non, so that the original judgment is reversed


Example: Assign a variable AA, determine whether the variable AA exists, the existence of the variable AA is determined to be equal to 30. Yes or no

[[email protected]/]# aa=30[[email protected]/]# [-N $aa-a $aa-eq] && echo "Yes" | | echo "No" yes

This article is from the "NJ Niche" blog, please be sure to keep this source http://liqingwen.blog.51cto.com/9853378/1913655

Shell Learning condition judgment test

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.