Shell checks whether a file exists #! /Bin/sh # shell checks whether the directory exists or has the permission 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"]; thenmkdir "$ mypath" fi # Here, the-D parameter determines whether $ mypath exists if [! -D "$ mypath"]; thenmkdir "$ mypath" fi # Here, the-F parameter determines whether $ myfile exists if [! -F "$ myfile"]; thentouch "$ myfile" fi # other parameters include-N, which determines whether a variable has a value if [! -N "$ myvar"]; thenecho "$ myvar is empty" Exit 0fi # determine whether two variables are equal if ["$ var1" = "$ var2"]; thenecho '$ var1 EQ $ var2 'elseecho' $ var1 not EQ $ var2' fi-F and-e differences 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 ornamed 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 aterminal 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.
Shell determines whether a file exists