Bash Commands - Comparison Operators
来源:互联网 发布:python socket 代理 编辑:程序博客网 时间:2024/05/16 10:23
use a different set of operators.
1. integer comparison
-eq is equal to
if [ "$a" -eq "$b" ]
if [ "$a" -ne "$b" ]
if [ "$a" -gt "$b" ]
-ge is greater than or equal to
if [ "$a" -ge "$b" ]
if [ "$a" -lt "$b" ]
if [ "$a" -le "$b" ]
(("$a" < "$b"))
<= is less than or equal to (within double parentheses)
(("$a" <= "$b"))> is greater than (within double parentheses)
(("$a" > "$b"))>= is greater than or equal to (within double parentheses)
(("$a" >= "$b"))2. string comparison
= is equal toif [ "$a" = "$b" ]
Note the whitespace framing the =.
if [ "$a"="$b" ] is not equivalent to the above.
if [ "$a" == "$b" ]
This is a synonym for =.
The == comparison operator behaves differently within a double-brackets test than
within single brackets.
[[ $a == z* ]]
# True if $a starts with an "z" (pattern matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).
[ $a == z* ]
# File globbing and word splitting take place.
[ "$a" == "z*" ] # True if $a is equal to z* (literal matching).
!= is not equal to
if [ "$a" != "$b" ]This operator uses pattern matching within a [[ ... ]] construct.
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
Note that the "<" needs to be escaped within a [ ] construct.
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
Note that the ">" needs to be escaped within a [ ] construct.
-z string is null, that is, has zero length
String=''
# Zero-length ("null") string variable.
if [ -z "$String" ]
then
echo "\$String is null."
else
echo "\$String is NOT null."
fi
# $String is null.
-n string is not null.
The -n test requires that the string be quoted within the test brackets. Using anunquoted string with ! -z, or even just the unquoted string alone within test brackets
normally works, however, this is an unsafe practice. Always quote a
tested string. [37]
- Bash Commands - Comparison Operators
- Bash Commands - File test operators
- 14.9.1 Integer comparison operators
- 14.9.3 Decimal comparison operators
- 14.9.5 Enumeration comparison operators
- 14.9.2 Floating-point comparison operators
- How to override comparison operators in Python
- Smallest of three integers without comparison operators
- Compare two ints without using comparison operators
- Bash Commands - Special Charactors # ? $
- Bash Commands - $ # Variable Substitution
- Bash Commands - tr cat tac
- Bash Commands - chmod # Invoking script
- Bash Commands - # Integer or string?
- Bash Commands - Arithmetic Tests using (( ... ))
- running bash commands in python
- Learn BASH -- Grouping Commands(from bash manual)
- Variable Mangling in Bash with String Operators
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
- hdu 4363 Draw and paint
- 在Silverlight里使用Enter事件
- poj 2485 解题报告
- js的函数的深度分析
- Bash Commands - Comparison Operators
- Eclipse快捷键 10个最有用的快捷键
- bat 运行java
- 用C#操作Oracle数据库的连接
- 骗子的最新把戏
- OmniORB 的简单使用例子
- Annotation入门实例,一分钟了解Annotation
- Java读取文件内容到byte[]数组中
- Apache mod_proxy通过https访问后端服务器的配置