Basic usage of strings in the shell

Source: Internet
Author: User

Objective

Today, when writing a script, found that the previous phase used some of the usage is still need to go to Baidu Search, and find the answer also need to filter, delay writing script time, this article on the comparison between the string and logical judgments are very detailed, reference to other people's work, resource sharing.

This film mainly describes some of the considerations in string comparisons

The main include string equality, unequal, length, empty string, and logical and logical, or the content, see the following script:

[Plain]View PlainCopy
  1. #!/bin/bash
  2. # test String Order
  3. A= "SS"
  4. b= "ABC"
  5. c= "SS"
  6. echo "----------------------------------------------------------------------------"
  7. echo "Test for string compare with \"!=\ ", \" =\ ", \"-z\ ", \"-n\ ", was a null str; test string is \" $a \ "and \" $b \ ""
  8. echo "----------------------------------------------------------------------------"
  9. echo "*************----------*********************"
  10. echo "-----------"
  11. echo "Test \" =\ "equals arithmetic, compares two strings equal"
  12. echo "-----------"
  13. echo "if [\ $a = \ $b]; Then use = to compare two strings are equal, note is not the = = symbol, note that the space symbol in the statement must be reserved, some of the following statements in the space format and the sentence similar to the "
  14. if [$a = $b]; Then
  15. echo "$a = = $b: a = B"
  16. Else
  17. echo "-----------"
  18. echo "Test \" "!=\", not Equal, compares two strings of unequal "
  19. echo "-----------"
  20. if [$a! = $c]; Then
  21. echo "$a = = $c: a!=c"
  22. Else
  23. echo "$a = = $c: a ==c"
  24. Fi
  25. Fi
  26. echo "-----------"
  27. echo "Test \"-z\ ", string length is 0 operation, 0 returns True"
  28. echo "-----------"
  29. If [-Z $a]
  30. Then
  31. echo "-Z $a: string length 0"
  32. Else
  33. echo "-Z $a: string length is not 0"
  34. Fi
  35. echo "-----------"
  36. echo "Test \"-n\, string length not 0 operation, not 0 return true "
  37. echo "-----------"
  38. If [-N $a]
  39. Then
  40. echo "-N $a: string length not 0"
  41. Else
  42. echo "-N $a: string length 0"
  43. Fi
  44. echo "-----------"
  45. echo "Test is a null str"
  46. echo "-----------"
  47. If [$a]
  48. Then
  49. echo "$a: String not empty"
  50. Else
  51. echo "$a: string is empty"
  52. Fi
  53. A= "AAA"
  54. b= "BBB"
  55. echo "----------------------------------------------------------------------------"
  56. echo "test string with \" &&\ ", \" | | \ "; Test string is \" $a \ "and \" $b \ ""
  57. echo "----------------------------------------------------------------------------"
  58. echo "-----------"
  59. echo "Test \" &&\ ", Logic and Operations"
  60. echo "-----------"
  61. if [[$a = $a && $b = $b]]
  62. Then
  63. echo "[[\ $a = \ $a && \ $b = \ $b]] returns True"
  64. Else
  65. echo "[[\ $a = \ $a && \ $b = \ $b]] returns false"
  66. Fi
  67. echo "-----------"
  68. echo "Test \" | | \ ", logical OR operation"
  69. echo "-----------"
  70. if [[$a = $b | | $b = $b]]
  71. Then
  72. echo "[[\ $a = \ $b | | \ $b = \ $b]] returns True "
  73. Else
  74. echo "[[\ $a = \ $b | | \ $b = \ $b]] returns false "
  75. Fi
  76. echo "*************----------*********************"

Basic string usage in the shell

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.