Shell script code that determines whether a file exists

Source: Internet
Author: User
Tags bit set
Implementing code One,

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" fi# here the-d parameter to determine if $mypath exists if [!-D "$myPath"]; Then mkdir "$myPath" FI # Here's the-f parameter to determine if $myfile exists if [!-F "$myFile"]; Then touch "$myFile" fi# other parameters and-n,-n is to determine if a variable has a value if [!-n "$myVar"]; Then echo ' $myVar is empty ' exit 0fi # Two variables determine if equal if ["$var 1" = "$var 2"]; Then echo ' $var 1 eq $var 2 ' else echo ' $var 1 not EQ $var 2 ' fi

Implementation code Two,

#shell判断文件夹是否存在 #如果文件夹不存在, create a folder if [!-D "/myfolder"]; Then Mkdir/myfolderfi #shell判断文件 whether the directory exists or has permissions folder= "/var/www/" file= "/var/www/log" #-x parameter to determine $folder Whether it exists and has enforceable permissions if [!-X ' $folder]; Then mkdir the "$folder" FI #-D parameter to determine if the $folder exists if [!-D "$folder"]; Then mkdir the "$folder" FI #-F parameter to determine if $file exists if [!-F "$file"]; Then touch "$file" fi #-N to determine if a variable has a value if [!-n "$var"]; Then echo "$var is empty" Exit 0fi # Determine if two variables are equal if ["$var 1" = "$var 2"]; Then echo ' $var 1 eq $var 2 ' else echo ' $var 1 not EQ $var 2 ' fi-f and-e difference conditional Logic on files-a file Exists.-b file Exi STS and is a block special File.-c file exists and are a character special file.-d file exists and is a directory.-e file E Xists (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 Exis TS 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 li Nk.-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 are a first in, first Out (FIFO) Special fi Le ornamed pipe.-r file exists and is readable by the current Process.-s file exists and have a size greater than zero.-s F Ile exists and is a socket.-t file descriptor number Fildes are open and associated with aterminal Device.-u file exists an D have its setuid (2) bit set.-w file exists and are writable by the current Process.-x file exists and are executable by the Current Process.-z string length is zero. The difference between-s or with-F is great!


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.