Copy Code code as follows:
#!/bin/sh
Mypath= "/var/log/httpd/"
Myfile= "/var/log/httpd/access.log"
#这里的-x parameter to determine whether $mypath exists and has executable permissions
if [!-X "$myPath"]; Then
mkdir "$myPath"
Fi
#这里的-D parameter to determine whether $mypath exists
if [!-D "$myPath"]; Then
mkdir "$myPath"
Fi
#这里的-F parameter to determine whether $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 Control ' if ' expression executes the part following then if the condition is true: if ...; Then
....
Elif ...; Then
....
Else
....
Fi
In most cases, you can use test commands to test conditions. For example, you can compare strings, determine whether a file exists and whether it is readable, and so on ... You typically use [] to represent a conditional test. Note that the spaces here are important. To ensure that the square brackets are blank.
[f "Somefile"]: To determine whether 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 users can read 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 is true for directory
-C file is true for character special files
-B file is a block special file is True
-S file file non-0 o'clock is true
-T file True when the device specified by the file descriptor (default is 1) is a terminal
#########################################################
Shell script with conditional selection
Simple shell scripts are generally competent for tasks that do not contain variables. However, in the implementation of a number of decision-making tasks, it is necessary to include the if/then of the conditional judgment. Shell scripting supports this type of operation,
Including comparison operations, to determine whether the existence of files. The basic if Condition command options are:-eq-compare two arguments for equality (for example, if [2–eq 5])
-ne-compare two arguments for unequal
-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 the directory exists
Almost all judgements can be implemented using these comparison operators. The common-f command option in a script checks to see if it exists before executing a file. ################################################################## determine if a file exists
Copy Code code as follows:
#!/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" linuxcy@126.com fi