My Way on Linux-[Shell Basics]-a common method of judging whether a file, directory exists, or whether it has a certain class of attributes (permissions) in the Bash shell

Source: Internet
Author: User
Tags bit set readable

Conditional Logic on Files # Determine if the file exists and the file type
-Afileexists. #文件存在-BfileExists and is a block specialfile. #文件存在, and it's a block device.-Cfileexists and is a character specialfile. # #文件存在, and is a character device-Dfileexists and is a directory. #文件存在, and is the directory-EfileExists (just the same as-a). #文件存在-Ffileexists and is a regularfile. #文件存在, and is an ordinary file-Lfileexists and is a symbolic link. #文件存在, and Symbolic links-Pfileexists and is a firstinch, first Out (FIFO) Specialfileor named pipe. #文件存在, and is a piping or FIFO device-Sfileexists and is a socket. #文件存在, and is a socket (socket) file-Sfileexists and has a size greater than zero. #文件存在, and file non-0 (greater than 0)
# Determine if the file exists and has the properties of the relevant permission
# General Properties-Rfileexists and is readable by the current process. #文件存在, and this process is readable-W    fileexists and is writable by the current process. #文件存在, and this process can be written-Xfileexists and is executable by the current process. #文件存在, and this process can execute # Extended Properties-Ufileexists and have its setuid (2) bit set. #文件存在, and has the Setuid property-Gfileexists and have its setgid (2) bit set. #文件存在, and has the Setgid property-Kfileexists and have its sticky bit set. #文件存在, and has sticky bits-Gfileexists and have the same group ID as this process. #文件存在, and has the same GID as this process-Ofileexists and is owned by the user ID of this process. #文件存在, and has the same UID as this process
# character length judgment
-Z    string  length is zero.    #字符串长度为零返回true-N    string length is not zero.    #字符串长度非零返回true
# Other options ( this place is not very understanding, please enlighten me )
-T    file Descriptor number Fildes is open and associated with a terminal device.    # file descriptor?? is open and is related to the terminal device- o    Named option is set on.    # turn on the naming option??
# Two comparisons between files
-NT    #newer than, to determine whether file1 is newer than file2, by comparing the time stamp of the file to achieve -ot    #older than, to determine whether File1 is older than file2, by comparing the timestamp implementation of the file /c2>-ef    file, to determine whether File2 and File2 is the same file, by judging the two files pointing to the inode is the same to achieve, can be used in hard link decision
# Two integers in a comparison
-eq    #equal  equals -ne    #not equal  not equal -GT    #greater than  greater than - Lt    #  less than  smaller than -ge    #greater than or equal  greater than or equal to-le    #  less than or equal  smaller than or equal to
# Multiple conditional judgments
-a    #and logic and, when both conditions are true--o    #or logic or, if any of the two conditions istrue!     #not logical non, reverse state returns True
Shell Examples of common judgments
#!/bin/Bashmypath="/var/log/nginx/"myfile="/var/log/nginx/access.log"# -The x parameter determines whether the $mypath exists and has executable permissionsif [!-X"$myPath"];ThenMkdir"$myPath"Fi# -The D parameter determines whether the $mypath exists, and the attribute is a directoryif [!-D"$myPath"];ThenMkdir"$myPath"Fi# -The f parameter determines whether the $myfile exists, and the attribute is a normal fileif [!-F"$myFile" ];ThenTouch"$myFile"Fi# -The n parameter determines whether a variable has a valueif [!-N"$myVar" ];ThenEcho"$myVar is empty"exit 0fi# Two variables determine if equal if [ "$var 1"= "$var 2"]; thenecho '$var 1 eq $var 2'elseecho '$var 1 not EQ $var 2'fi  

My Way on Linux-[Shell Basics]-a common method of judging whether a file, directory exists, or whether it has a certain class of attributes (permissions) in the Bash shell

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.