Shell script----if (numeric condition, string condition, string null)

Source: Internet
Author: User

Binary comparison operators, comparing variables or comparing numbers.

Note the difference between a number and a string.

1. Integer comparison

[CPP]View PlainCopyprint?
  1. -eq equals, such as:if [ "$a"-eq " $b"]
  2. -ne not equal to, such as:if [ "$a"-ne " $b"]
  3. -GT greater than, such as:if [ "$a"-gt " $b"]
  4. -ge greater than equals, such as:if [ "$a"-ge " $b"]
  5. -lt less than, such as:if [ "$a"-lt " $b"]
  6. -le less than equals, such as:if [ "$a"-le " $b"]
  7. < less (requires double brackets), such as: (("$a" < "$b" )
  8. <= is less than or equal (requires double brackets), such as: (("$a" <= " $b"))
  9. > Greater than (requires double brackets), such as: (("$a" > "$b" )
  10. >= greater than or equal (requires double brackets), such as: (("$a" >= " $b"))

Small data comparisons can be used with awk

2. String comparisons

[CPP]View PlainCopy print?
    1. = equals, such as:if [ "$a" = "$b"]
    2. = = equals, such as:if [ "$a" = = " $b"], and = equivalent

Attention:

The way to compare two strings for equality is if ["$test" x = "Test" x]; Then here are the key points: 1 use a single equals sign 2 notice that there is a space on each side of the equal sign: This is the UNIX shell's requirement 3 notice that the "$test" x Last X, which is deliberately arranged, because when $test is empty, the expression above becomes x = Testx, is obviously not equal. Without this x, the expression would be an error: [: =: unary operator expected


Note: The behavior of the = = function is different in [[]] and [], as follows:
[CPP]View PlainCopyprint?
    1. [[$a = = z*]] # if $ A starts with "Z" (pattern match) then true
    2. [[$a = = "z*"]] # if $ A equals z* (character match) Then the result is true
    3. [$a = = z*] # File globbing and word splitting will occur
    4. [ "$a" = = "z*"] # if $ A equals z* (character match) Then the result is true


A little explanation, about file globbing is a shorthand for the document, such as "*.c" is, again like ~ also.
But file globbing is not a strict regular expression, although in most cases the structure is more like.
! = does not equal, such as: if ["$a"! = "$b"]
This operator will use pattern matching in the [[]] structure.
< less than, in ASCII alphabetical order. such as:
if [["$a" < "$b"]
If ["$a" \< "$b"]
Note: the "<" in the [] structure needs to be escaped.
> Greater than, in ASCII alphabetical order. such as:
if [["$a" > "$b"]
If ["$a" \> "$b"]
Note: the ">" in the [] structure needs to be escaped.
Refer to Example 26-11 for an example of this operator application.
The-Z string is "null". The length is 0.
-N string not "null"
Attention:
The use of-N in the [] structure must be used to test the variable. Use a string that is not ""! -Z
Or the string itself, which is not referenced by "", is placed in the [] structure. Although it is generally possible to

To work, but it's not safe. It's a good habit to use "" to test strings.

Example:

1. Comparison of numbers

[CPP]View PlainCopyprint?
  1. #!/bin/bash
  2. I=6
  3. a=10
  4. If [$a-eq 10]
  5. Then
  6. Echo "A = ten"
  7. Fi
  8. If [$a-ne $i]
  9. Then
  10. Echo "A! = $i"
  11. Fi
  12. If [$a-gt $i]
  13. Then
  14. echo "a > i"
  15. Fi
  16. If [$a-lt $i]
  17. Then
  18. Echo "A < I"
  19. Else
  20. echo "a > i"
  21. Fi
  22. if (("$a" > "$i")
  23. Then
  24. Echo "(()) a>i"
  25. Fi
  26. if ($a! = $i))
  27. Then
  28. Echo "(()) a!=i"
  29. Fi

Note: Run script through SH, [] operation is OK, and (()) Run error

chmod after 777, direct./Run, all can

2. String comparisons

[CPP]View PlainCopyprint?
  1. #!/bin/bash
  2. A="123"
  3. b="1234"
  4. c="123"
  5. If [ "$a" x! = "$b" x]
  6. Then
  7. Echo "A! = B"
  8. Fi
  9. If [ "$a" x = "$c" x]
  10. Then
  11. Echo "A = = C"
  12. Fi

Determine if the string is empty

[CPP]View PlainCopyprint?
    1. If [-Z "$d"]
    2. Then
    3. Echo "D is empty"
    4. Fi


Note:

-E File exists
-A file exists (deprecated)
-F test file is a regular file (normal file, non-directory or device)
-S file length is not 0
-D is the directory of the object being tested
-B The object being measured is a block device
-c The object being measured is a character device
-P The object being measured is the pipe
-h The measured file is a symbolic connection
-L test file is a symbolic connection
-S (uppercase) the file being measured is a socket
-T relates a file descriptor to an end device. The stdin[-t0] or [-T1] that is used to detect the script is a terminal
-r file has read permissions for the user running the script
-W file has write permissions for the user running the script
-X file has execute permissions for the user running the script
-U Set-user-id (SUID) flag to file, which is the root permission file that can be used by ordinary users, through chmod +s file implementation
-K Set Paste bit
-o The user who runs the script is the owner of the file
-G file Group-id the same as the user running the script
-N is read from the end of the file to the present, whether it is modified

F1-nt F2 file F1 is newer than F2
F1-ot F2 file F1 is older than F2
F1-ef f2 files F1 and F2 are hard-wired to the same file

Shell script----if (numeric condition, string condition, string null)

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.