About conditional expressions in shell scripts

Source: Internet
Author: User

One, the file operation
1. Expression
-D: Whether it is a directory
-F: Whether it is a file
-E: Whether the file or directory exists
-R: Whether the current user has Read permissions
-W: Whether the current user writes permissions
-X: Whether the current user has executable permissions

2. Example (using the test command at the command line or [])

[[email protected] ~]# test-d/home/oldcat/tools/[[email protected] ~]# echo $?0[[email protected] ~]# test-d/HOME/OLDC at/tools/redis-3.0.7.tar.gz [[email protected] ~]# echo $?1[[email protected] ~]# test-f/home/oldcat/tools/redis-3.0.7 . tar.gz [[email protected] ~]# echo $?0 Description: Output execution result is 0 true, reverse false

Second, numerical comparison
1. Expression

-EQ: Two operands are equal
-ne: Whether the two operands are not equal
-le: Whether the left operand is less than or equal to the right operand
-ge: Whether the left operand is greater than or equal to the right operand
-LT: Whether the left operand is less than the right operand
-GT: Whether the left operand is greater than the right operand

2. Examples and explanations

[[email protected] ~]# test 1-eq 2[[email protected] ~]# echo $?1[[email protected] ~]# test 1-eq 1[[email protected] ~] # echo $?0[[email protected] ~]# test 2-gt 1[[email protected] ~]# echo $?0[[email protected] ~]# test 1-ge 1[[email Pro Tected] ~]# echo $?0 Description: Output execution result is 0 true, reverse is false

Three, string comparison

1. Expression
=: two strings are equal
! =: Two field strings are not equal
-Z: Determines whether the field string is empty

2. Examples and explanations

[Email protected] ~]# Test "CHENFL" = "CHENFL" [[email protected] ~]# echo $?1[[email protected] ~]# test "CHENFL"! = "Che NFL "[[email protected] ~]# echo $?1[[email protected] ~]# test" CHENFL "=" CHENFL "[[email protected] ~]# echo $?0[[email p Rotected] ~]# test-z "" [[email protected] ~]# echo $?0[[email protected] ~]# test-z "CHENFL" [[email protected] ~]# Echo $?1 Description: Output execution result is 0 true, reverse is false

Iv. Logical Judgment
1. Expression
&&: Logic and, only the first and the two expressions are true execution result is true (0), otherwise false (1)
|| : Logical OR, before and after two expressions as long as one is true, the execution result is true (0), otherwise false (1)
!: logical non If the expression execution result is True then false (1), if the expression execution result is false that is true (0)

2. Examples and explanations

[[Email protected] ~]# [1-eq 1] && ["hehe"! = "hehe"][[email protected] ~]# echo $?1[[email protected] ~]# [ 1-eq 1] | | ["hehe"! = "hehe"] [[email protected] ~]# echo $?0[[email protected] ~]# [!-d/home/oldcat/tools/][[email protected] ~]# echo $?1 Description: Output execution result 0 is true, and the reverse is false







About conditional expressions in shell scripts

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.