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 whether a file [-X "/bin/ls"]: Determine whether the/bin/ls exists and have executable permissions [-n "$var]: Determine whether the $var variable has a value [" $a "=" $b "]: Determine whether a $ 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 File as a normal file for true-D file files for the directory for true-C file file for character special files for true-B file files for block special files for true-s file files non-0 o'clock for true-t The file descriptor (default is 1) specifies that the device is true when the terminal is ######################################################### shell script with conditional selection simple shell scripts for tasks that do not contain variables are generally capable. 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-Compare two parameters are unequal-lt-parameter 1 is less than parameter 2-le-parameter 1 is less than equals parameter 2-gt-parameter 1 is greater than parameter 2-ge-parameter 1 is Greater than or equal to the parameter 2-f-checks if a file exists (for example, if [-F "filename"]) the D-Check directory exists almost all of the 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/shtoday= ' date-d Yesterday +%y%m%d ' file= "apache_$today.tar.gz" Cd/home/chenshuo/shellif [-F "$file"];thenecho "OK" Elseecho "error $ File ">error.logmail-s" fail backup from Test "[email protected] <error.logfi
Shell if else and greater than, less than equal logical expression