The shell determines whether a file or directory exists

Source: Internet
Author: User
Tags bit set

Shell Determines whether a file exists1. Shell to determine the file, whether the directory exists or has permissions
2. #!/bin/sh
3.
4. Mypath= "/var/log/httpd/"
5. Myfile= "/var/log/httpd/access.log"
6.
7. # the-x parameter here determines whether the $mypath exists and has executable permissions
8. If [!-X "$myPath"]; Then
9. mkdir "$myPath"
Ten. Fi
One by one .
12. # The-d parameter here determines whether $mypath exists
if [!-D "$myPath"]; Then
mkdir "$myPath"
. Fi
.
17. # Here's the-f parameter to determine if $myfile exists
if [!-F "$myFile"]; Then
touch "$myFile"
. Fi
.
22. # Other parameters also have-n,-n is to determine whether a variable has a value
if [!-n "$myVar"]; Then
echo "$myVar is empty"
Exit 0
. Fi
.
28. # Two variables are judged equal
if ["$var 1" = "$var 2"]; Then
echo ' $var 1 eq $var 2 '
. Else
Echo ' $var 1 not EQ $var 2 '
. Fi

the difference between-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-a).
-f file exists and is a regular file.
-G file exists and have its setgid (2) bit set.
-G file exists and have the same group ID as this process.
-K file exists and have 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 a
terminal device.
-u file exists and have 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.

is the difference with-s or with-f this is very big!  transferred from: http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html

The shell determines whether a file or directory exists

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.