KSH comparison operator

String Comparison

[[ str1 == pattern ]]       str1 = pattern,
[[ str1 = pattern ]]        Same as == above, but is obsolete
[[ str1 != pattern ]]       str1 != pattern
[[ str1 < str2 ]]           str1 precedes str2
[[ str1 > str2 ]]           str1 follow str2
[[ -z str ]]                str is null, it includes following 3 format:
                                 str=
                                 str=""
                                 unset str
[[ -n str ]]                str is not null

Sample 1

[[ ${str} == ABC* ]];                        # str start with ABC
[[ ${str} = *DEF ]];                         # str end with DEF
[[ ${str} != *DEF ]];                        # str NOT end with DEF
[[ ${str} = @(ABC*|*DEF) ]];                 # str start with ABC or end with DEF
[[ ${str} == ABC* || ${str} == *DEF) ]];     # str start with ABC or end with DEF

Sample 2

VAR1="12"
VAR2="34"
VAR3="12_34"

VARID="3"

if [[ ${VAR1}_${VAR2} = @(${VAR3}) ]]; then

if eval [[ ${VAR1}_${VAR2} = @(\$\{VAR${VARID}\}) ]]; then

Number Comparison

(( num1 == num2 ))           numbers equal
(( num1 != num2 ))           numbers not equal
(( num1 < num2 ))            num1 < num2
(( num1 > num2 ))            num1 > num2
(( num1 <= num2 ))           num1 <= num2
(( num1 >= num2 ))           num1 >= num2

The following obsolete arithmetic comparisons are also permitted:

exp1 -eq exp2                exp1 is equal to exp2.
exp1 -ne exp2                exp1 is not equal to exp2.
exp1 -lt exp2                exp1 is less than exp2.
exp1 -gt exp2                exp1 is greater than exp2.
exp1 -le exp2                exp1 is less than or equal to exp2.
exp1 -ge exp2                exp1 is greater than or equal to exp2.

File Comparison

-c file              True, if file exists and is a character special file.
-d file              True, if file exists and is a directory.
-e file              True, if file exists.
-f file              True, if file exists and is an ordinary file.
-r file              True, if file exists and is readable by current process.
-s file              True, if file exists and has size greater than zero.
-w file              True, if file exists and is writable by current process.
-x file              True,  if file exists and is executable by current process.
-L file              True, if file exists and is a symbolic link.
-h file              True, if file exists and is a symbolic link.

Logical Operator

[[ expr1 && expr2 ]]
[[ expr1 || expr2 ]]
[[ ! expr1 ]]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值