The If else of the shell and the logical expression greater than, less than, etc.

Source: Internet
Author: User
Tags readable

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

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.