Use the if statement to determine parameters in shell programming
-B. returns true if the file exists and is a block file.
-C: returns true if the file exists and is a character file.
-D. If pathname exists and is a directory, true is returned.
-E: returns true if the file or directory specified by pathname exists.
-F returns true if the file exists and is a regular file
-G returns true if the file or directory specified by pathname exists and the SGID bit is set.
-H returns true if the file exists and is a symbolic link file. This option is invalid in some old systems.
-K. If a file or directory specified by pathname exists and a "Sticky" bit is set, the system returns the true value.
-P: returns true if the file exists and is a command pipeline.
-R: returns true if the file or directory specified by pathname exists and is readable.
-S: returns true if the file size is greater than 0.
-U returns true if a file or directory specified by pathname exists and SUID is set.
-W returns true if the file or directory specified by pathname exists and is executable. A directory must be executable for its content access.
-O returns true if a file or directory specified by pathname exists and the user specified by the valid user ID of the current process is owned by it.
Comparison character writing in UNIX Shell:
-Eq equals
-Ne is not equal
-Gt greater
-Lt is less
-Le is less than or equal
-Ge is greater than or equal
-Z empty string
= Two equal characters
! = Two characters
-N non-empty string
-------------------------------------------------------------------------
For more details:
Operator Description Example
File comparison operator
-E filename if filename exists, it is true [-e/var/log/syslog]
-D filename if filename is a directory, it is true [-d/tmp/mydir]
-F filename if filename is a regular file, it is true [-f/usr/bin/grep]
-L filename if filename is a symbolic link, it is true [-L/usr/bin/grep]
-R filename if filename is readable, it is true [-r/var/log/syslog]
-W filename if filename is writable, it is true [-w/var/mytmp.txt]
-X filename if filename is executable, it is true [-L/usr/bin/grep]
Filename1-nt filename2 if filename1 is newer than filename2, it is true [/tmp/install/etc/services-nt/etc/services]
Filename1-ot filename2 if filename1 is earlier than filename2, true [/boot/bzImage-ot arch/i386/boot/bzImage]
Note the use of quotation marks for string comparison operators. This is a good way to prevent space from disturbing the code)
-Z string if the string length is zero, it is true [-z $ myvar]
-N string if the string length is non-zero, it is true [-n $ myvar]
String1 = string2 if string1 is the same as string2, it is true [$ myvar = one two three]