在Shell脚本中,比较运算符的使用是非常常见的,特别是在条件判断时。为了更巧妙地记忆这些运算符,可以结合英文单词的缩写和简单的联想记忆法(越简单越容易记)。
1、首先记 “等于” 和 “不等于”
等于:-eq
(equal)
不等于:-ne
(not equal)
2、其次记 “大于” 和 “小于”
大于:-gt
(greater than)
小于:-lt
(less than)
3、最后记 “大于或等于” 和 “小于或等于”
大于或等于:-ge
(greater than or equal)
小于或等于:-le
(less than or equal)
4、汇合
-eq # 等于(equal)
-ne # 不等于(not equal)
-gt # 大于(greater than)
-lt # 小于(less than)
-ge # 大于或等于(greater than or equal)
-le # 小于或等于(less than or equal)
记住它们对应的英文比较简单,然后自己能在本子手写一遍,使用时多联想对应的单词是什么,基本上就记住了。
拓展:除了以上比较常见的6种,PowerShell 还支持许多其他类型的运算符。
运算符 | 描述 | 示例 |
---|---|---|
-like | 模式匹配 | $a -like "a*" |
-notlike | 不匹配模式 | $a -notlike "a*" |
-match | 正则匹配 | $a -match "\d+" |
-notmatch | 不匹配正则 | $a -notmatch "\d+" |