当前位置:网站首页>Shell basic operators -- relational operators

Shell basic operators -- relational operators

2022-06-24 08:37:00 Chen Bucheng I

Relational operator

Relational operators only support numbers , String not supported , Unless the value of the string is a number . The following table lists the common relational operators , Assumed variable a by 10, Variable b by 20

Operator

explain

give an example

-eq

Check whether two numbers are equal , Equal return true.

[ $a -eq $b ] return false.

-ne

Check if two numbers are not equal , Unequal return true.

[ $a -ne $b ] return true.

-gt

Check whether the number on the left is greater than that on the right , If it is , Then return to true.

[ $a -gt $b ] return false.

-lt

Check if the number on the left is less than the number on the right , If it is , Then return to true.

[ $a -lt $b ] return true.

-ge

Check whether the number on the left is equal to or greater than the number on the right , If it is , Then return to true.

[ $a -ge $b ] return false.

-le

Check whether the number on the left is less than or equal to the number on the right , If it is , Then return to true.

[ $a -le $b ] return true.

Examples of relational operators are as follows :

  1. #!/bin/bash
  2. a=10
  3. b=20
  4. if[ $a -eq $b ]
  5. then
  6. echo "$a -eq $b : a be equal to b"
  7. else
  8. echo "$a -eq $b: a It's not equal to b"
  9. fi
  10. if[ $a -ne $b ]
  11. then
  12. echo "$a -ne $b: a It's not equal to b"
  13. else
  14. echo "$a -ne $b : a be equal to b"
  15. fi
  16. if[ $a -gt $b ]
  17. then
  18. echo "$a -gt $b: a Greater than b"
  19. else
  20. echo "$a -gt $b: a No more than b"
  21. fi
  22. if[ $a -lt $b ]
  23. then
  24. echo "$a -lt $b: a Less than b"
  25. else
  26. echo "$a -lt $b: a Not less than b"
  27. fi
  28. if[ $a -ge $b ]
  29. then
  30. echo "$a -ge $b: a Greater than or equal to b"
  31. else
  32. echo "$a -ge $b: a Less than b"
  33. fi
  34. if[ $a -le $b ]
  35. then
  36. echo "$a -le $b: a Less than or equal to b"
  37. else
  38. echo "$a -le $b: a Greater than b"
  39. fi

Execute the script , The output is as follows :

  1. 10-eq 20: a It's not equal to b
  2. 10-ne 20: a It's not equal to b
  3. 10-gt 20: a No more than b
  4. 10-lt 20: a Less than b
  5. 10-ge 20: a Less than b
  6. 10-le 20: a Less than or equal to b
原网站

版权声明
本文为[Chen Bucheng I]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/06/20210622172020437e.html