Shell judge file, directory exists or has permission (reprint)

Source: Internet
Author: User

Transferred from: http://cqfish.blog.51cto.com/622299/187188

Article Source: http://hi.baidu.com/haigang/blog/item/e5f582262d639c118b82a167.html#!/bin/sh
Mypath= "/var/log/httpd/"
Myfile= "/var/log/httpd/access.log"
#这里的-x parameter determines whether $mypath exists and has executable permissions
if [!-X "$myPath"]; Then
mkdir "$myPath"
Fi
#这里的-D parameter to determine if $mypath exists
if [!-D "$myPath"]; Then
mkdir "$myPath"
Fi
#这里的-F parameter to determine if $myfile exists
if [!-F "$myFile"]; Then
Touch "$myFile"
Fi
#其他参数还有-n,-n is to determine whether a variable has a value
if [!-n "$myVar"]; Then
echo "$myVar is empty"
Exit 0
Fi
#两个变量判断是否相等
If ["$var 1" = "$var 2"]; Then
Echo ' $var 1 eq $var 2 '
Else
Echo ' $var 1 not EQ $var 2 '
Fi

========================================
Shell Judgment Statement

The process controls the "if" expression if the condition is true then the following part is executed: if ...; Then
....
Elif ...; Then
....
Else
....
Fi
In most cases, you can use test commands to test the condition.   For example, you can compare strings, determine whether the file exists and whether it is readable, etc... The condition test is usually represented by "[]". Note that the space here is important. The space to ensure the square brackets.
[-F "somefile"]: Determine if it is a file
[-X "/bin/ls"]: Determine if/bin/ls exists and has executable permissions
[-N ' $var]: Determine if the $var variable has a value
["$a" = "$b"]: Determine if $ A and $b are equal-r file user readable as True
-W file user can write as true
-X file user can execute as true
-F file is true for regular files
-d file files are directory-True
-C File file is true for character special files
-B file files are true for block special files
-S file files non-0 o'clock True
-T file is true when the specified device is terminal (default = 1)
######################################################### shell script with conditional selection
Simple shell scripts are generally competent for tasks that do not contain variables. However, when you perform some decision-making tasks, you need to include the if/then criteria to judge. Shell scripting supports such operations, including comparison operations, determining whether a file exists, and so on. The basic if Condition command options are:-eq-compare two parameters for equality (for example, if [2–eq 5])
-ne-comparison of two parameters is not equal
-lt-parameter 1 is less than parameter 2
-le-parameter 1 is less than or equal to parameter 2
-gt-parameter 1 is greater than parameter 2
-ge-parameter 1 is greater than or equal to parameter 2
-f-Check if a file exists (for example, if [-F "filename"])
-D Check if directory exists
Almost all judgments can be implemented with these comparison operators. The common-f command option in a script checks to see if it exists before executing a file. ################################################################## determine if the file exists #!/bin/sh
Today= ' date-d yesterday +%y%m%d '
File= "Apache_$today.tar.gz"
Cd/home/chenshuo/shell
If [-F "$file"];then
echo "OK"
Else
echo "Error $file" >error.log
Mail-s "fail backup from Test"[email protected]<error.log
Fi

Shell judge file, directory exists or has permission (reprint)

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.