Shell condition test

Source: Internet
Author: User
Tags builtin
When writing a script in a shell condition test, you may need to check the file status or number test to determine whether the string is equal. Further actions can be performed based on these tests. 1. the operator of the file test and test file status is as follows:-d Directory-s file length is greater than 0, non-empty-f regular file... information & n shell condition test when writing a script, sometimes it is necessary to determine whether the string is equal, may also check the file status or number test. Further actions can be performed based on these tests. 1. operators for file test and test file states are as follows: -d Directory-s file length is greater than 0, non-empty-f regular file-w writable-L symbolic connection-u file suid bit settings-r readable-x executable test used logical operators test whether the file status is OK, but sometimes two files must be compared. Shell provides three logical operations to complete this function. -Logic a and the operators are true on both sides, and the result is true. Otherwise, the result is false. -O logic or, if either side of the operator is true, the result is true. Otherwise, the result is false .! Logical no. the condition is false and the result is true. The following two files are compared: [root @ localhost ~] # Ls-l results.txt scores.txt-rwxr-xr-x 1 root 0 Nov 21 results.txt-rw-r -- 1 root 0 Nov 21 scores.txt test two examples whether the file is readable. [Root @ localhost ~] # [-W results.txt-a-w scores.txt] [root @ localhost ~] # Echo $? 0 returns true. 2. string testing is an important part of error capture, especially when testing user input or compare variables. There are three operators for string testing. = Two strings are equal! = Two strings without equal-z strings are null-n non-null strings. 3. many operators can be used to test the numerical value, which can be: -eq value equal-ne value not equal-gt first number greater than second number-lt first number less than second number-le first number less than or equal to second number-ge first number greater than or equal to second number returns the same results. Both are used to test whether the two numbers are equal (130 is equal to 130 ). [Root @ localhost ~] # NUMBER = 130 [root @ localhost ~] # ["$ NUMBER"-eq "130"] [root @ localhost ~] # Echo $? 0 is correct. You do not need to place the integer value into a variable. you can simply compare it with a number, but you need to enclose it with quotation marks. [Root @ localhost ~] # ["990"-le "995"] [root @ localhost ~] # Echo $? 0 can combine two test expressions with logical operators. You only need to use a pair of square brackets instead of two. Otherwise, the error message "too contains arguments" is returned ". [Root @ localhost ~] # ["990"-le "995"]-a ["123"-gt "33"]-bash: [: too using arguments the following example tests two expressions, if both are true and the result is true, the correct usage should be: [root @ localhost ~] # ["990"-le "995"-a "123"-gt "33"] [root @ localhost ~] # Echo $? 04. similarities and differences between test, [], [[], and () [[it is the keyword of bash program language, while others are built-in commands! $ Type [[[test [is a shell builtin [[is a shell keywordtest is a shell builtin. In most cases, these three functions are common. However, commands and keywords are always different. What is the difference between commands and keywords? If it is a command, it tests the parameter as a comparison expression or file, and returns an exit code based on the comparison result. For example, ">" <"is interpreted as a redirection symbol by shell. The keyword is not like this. The keyword regards the entire expression as a separate element and returns an exit code. The [[] structure is more flexible than [] and is an extended test command. () Can be applied to arithmetic expressions and the result is used as the exit code. If the expression result is 0, 1 is returned as the exit code; otherwise, 0 is returned as the exit code.
 
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.