Shell Boolean operation

Source: Internet
Author: User
1. The true and false values in bash are not the values 0 and 1 we generally think.
True and false are built-in shell commands that return logical values. For example:
$ True
$ Echo $?
0
$ False
$ Echo $?
1
$? Is a special variable that stores the ending state of the previous program.
In Shell, 0 indicates whether the program is successfully completed.
 
2. In shell, the logical value indicates the return value of the program. If the program returns successfully,
True. If the return is unsuccessful, the return value is false.
 
3. The test command can be used for numerical testing, string testing, and file testing.
* Numerical test:
$ If Test 5-EQ 5; then Echo "yes"; else echo "no"; FI
Yes
$ If Test 5-ne 5; then Echo "yes"; else echo "no"; FI
No
* String Testing
$ If test-n "not empty"; then Echo "yes"; else echo "no"; FI
Yes
-N: the length of the test string cannot be zero.
$ If test-z "not empty"; then Echo "yes"; else echo "no"; FI
No
-Z: the length of the test string is zero.
$ If test-z ""; then Echo "yes"; else echo "no"; FI
Yes
* File test
$ If test-F/boot/system. Map; then Echo "yes"; else echo "no"; FI
Yes
-F is used to test whether a file exists and whether it is a common file.
$ If test-D/boot/system. Map; then Echo "yes"; else echo "no"; FI
-D is used to test whether the file exists and whether it is a directory.
 
4. Test logical combination test
* And, for example:
$ A = 5; B = 4; C = 6;
$ If test $ A-EQ 5-A $ B-EQ 4-A $ C-EQ 6; then Echo "yes"; else echo "no"; FI
Yes
* Or, for example:
$ If test $ A-EQ 5-o $ B-ne 4; then Echo "yes"; else echo "no"; FI
Yes
* Not, for example:
$ If test! -F/etc/profile; then Echo "yes"; else echo "no"; FI
No
 
5. Test and &, |
$ Str1 = "test"; str2 = "test"
$ If test-z "$ str1"-o "$ str2" = "$ str1"; then Echo "yes"; else echo "no"; FI
Yes
$ If test-z "$ str1" | test "$ str1" = "$ str2"; then Echo "yes"; else echo "no"; FI
Yes
The two test statements are equivalent. But we can see that-O is used inside test, and | used between test
Test can be replaced by []. Note that spaces are required before and after [].
When testing a string, we recommend that all variables be enclosed in double quotation marks to prevent null strings represented by variables.
Note the following when testing with brackets:
$ I = 5
$ If [$ I = 6]; then Echo "yes"; else echo "no"; FI
Yes
$ If [$ I = 6]; then Echo "yes"; else echo "no"; FI
No
 
6. Command list
* For &, if the preceding command is successfully executed, continue to execute the following command; if it fails, do not execute the following command
* For |, if the preceding command is not successfully executed, continue to execute the following command.
For example:
Ping-C1 www.lzu.edu.cn & Echo "connected"

* The command list is sometimes used to replace branch structures such as if/then, so that some code can be omitted.


References

================================

Shell programming example -- by Falcon

Shell Boolean operation

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.