Shell script code to determine if a file exists _linux shell

Source: Internet
Author: User
Tags bit set mkdir


Implement code One,


#!/bin/sh # to
determine if a file exists
# link:www.jb51.net
# date:2013/2/28

mypath= '/var/log/httpd/
' myfile= ' Var/log/httpd/access.log "

# Here's the-x parameter to determine if $mypath exists and has executable permissions if
[!-X" $myPath "]; then
 mkdir" $myPath " C10/>fi
# Here the-d parameter determines if $mypath exists if
[!-D "$myPath"]; then
 mkdir "$myPath"
fi

# Here's the-F parameter to judge $ MyFile whether there is
an IF [!-F "$myFile"]; then touch
 "$myFile"
fi
# Other parameters and-n,-n is to determine whether a variable has a value if
[!-N ' $ MyVar "]; Then
 echo "$myVar is empty"
 exit 0
fi

# Two variables to determine whether equality if
["$var 1" = "$var 2"]; then
 Echo ' $var 1 eq $var 2 '
else
 Echo ' $var 1 don't eq $var 2 '
fi


Implementation code Two,


#shellDetermine if the folder exists

#If the folder does not exist, create the folder
if [! -D "/ myfolder"]; then
  mkdir / myfolder
fi

#shellJudge files, Whether the directory exists or has permissions  folder= "/var/www/"
file= "/var/www/log"

#-X parameters to determine if $folder exists and has executable permissions if
[! x "$ Folder "]; Then
 mkdir "$folder"
fi

#-D parameters to determine if $folder exists if
[!-D "$folder"]; then
 mkdir "$folder"
F

the I # f parameter determines whether the $file exists
if [!-F "$file"]; then touch
 "$file"
fi #-

n Determines whether a variable has a value
if [!-N "$var"]; Then
 echo "$var is empty"
 exit 0
Fi

# to determine if two variables are equal
if ["$var 1" = "$var 2"]; then
 Echo ' $var 1 EQ $var 2 '
else
 Echo ' $var 1 don't 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 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-a-a-in-a-a-i-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 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.



Is the difference between the-s or the-F is very big!


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.