Shell determines whether a file exists
The Code is as follows:
#! /Bin/sh
# Determining whether a file exists
# Link: www.jb51.net
# Date: 2013/2/28
MyPath = "/var/log/httpd /"
MyFile = "/var/log/httpd/access. log"
# Here, the-x parameter determines whether $ myPath exists and has executable permissions.
If [! -X "$ myPath"]; then
Mkdir "$ myPath"
Fi
# Here, the-d parameter determines whether $ myPath exists.
If [! -D "$ myPath"]; then
Mkdir "$ myPath"
Fi
# Here, the-f parameter determines whether $ myFile exists.
If [! -F "$ myFile"]; then
Touch "$ myFile"
Fi
# Other parameters include-n, which determines whether a variable has a value.
If [! -N "$ myVar"]; then
Echo "$ myVar is empty"
Exit 0
Fi
# Determine whether two variables are equal
If ["$ var1" = "$ var2"]; then
Echo '$ var1 eq $ var2'
Else
Echo '$ var1 not eq $ var2'
Fi
-F and-e
Conditional Logic on Files
-A file exists.
-B file exists and is a block special file.
-C file exists and is a character special file.
-D file exists and is a directory.
-E file exists (just the same as-).
-F file exists and is a regular file.
-G file exists and has its setgid (2) bit set.
-G file exists and has the same group ID as this process.
-K file exists and has its sticky bit set.
-L file exists and is a symbolic link.
-N string length is not zero.
-O Named option is set on.
-O file exists and is owned by the user ID of this process.
-P file exists and is a first in, first out (FIFO) special file or
Named pipe.
-R file exists and is readable by the current process.
-S file exists and has a size greater than zero.
-S file exists and is a socket.
-T file descriptor number fildes is open and associated with
Terminal device.
-U file exists and has its setuid (2) bit set.
-W file exists and is writable by the current process.
-X file exists and is executable by the current process.
-Z string length is zero.
Whether to use-s or-f is very different!