test 命令非常强大,但是很难满足其转义需求以及字符串和算术比较之间的区别。

大于 小于 等于 不等于
if [ "$a" -gt "$b" ] if [ "$a" -lt "$b" ] if [ "$a" -eq "$b" ] if [ "$a" -ne "$b" ]
if [[ "$a" -gt "$b" ]] if [[ "$a" -lt "$b" ]] if [[ "$a" -eq "$b" ]] if [[ "$a" -ne "$b" ]]
if ((“$a” > “$b”)) if ((“$a” < “$b”)) if ((“$a” == “$b”)) if ((“$a” != “$b”))

字符串比较

大于 小于 等于 不等于
if [ "$a" \> "$b" ] if [ "$a" \< "$b" ] if [ "$a" = "$b" ]
if [ "$a" == "$b" ]
if [ "$a" != "$b" ]
if [[ "$a" > "$b" ]] if [[ "$a" < "$b" ]] if [[ "$a" = "$b" ]]
if [[ "$a" == "$b" ]]
if [[ "$a" != "$b" ]]

[[ ]] and []

功能 [[ ]] [ ] 例子
字符串比较 > \>  -
=(or ==) =  -
!= !=  -
表达式组合 && -a [[ -n $var && -f $var ]] && echo “$var is a file”
|| -o  -
模式匹配
(Pattern matching)
=(or ==) 木有 [[ $name = "a*" ]] -> the string “a*”
正则匹配(RegularExpression matching) =~ 木有 [[ $(date) =~ ^Fri\ ...\ 13 ]] && echo “It’s Friday the 13th!”

特性

例子

文件或文件夹存在

-e

[[ -e $config ]] && echo ”config file exists: $config”

文件新旧比较

-nt/-ot

[[ $file0 -nt $file1 ]] && echo ”$file0 is newer than $file1″

同一个文件

-ef

[[ $input -ef $output ]] \ && { echo ”will not overwrite input file: $input”; exit 1; }

否定

!

-

逻辑组合判断的一些例子:
if [ $condition1 ] && [ $condition2 ]
if [ $condition1 -a $condition2 ]
if [[ $condition1 && $condition2 ]]
if [ $condition1 ] || [ $condition2 ]
if [ $condition1 -o $condition2 ]
if [[ $condition1 || $condition2 ]] # Also works.
#The &&, ||, operators work within a [[ ]] test, despite giving an error within a [ ] construct.

大于 小于 等于 不等于
if [ "$a" -gt "$b" ] if [ "$a" -lt "$b" ] if [ "$a" -eq "$b" ] if [ "$a" -ne "$b" ]
if [[ "$a" -gt "$b" ]] if [[ "$a" -lt "$b" ]] if [[ "$a" -eq "$b" ]] if [[ "$a" -ne "$b" ]]
if ((“$a” > “$b”)) if ((“$a” < “$b”)) if ((“$a” == “$b”)) if ((“$a” != “$b”))

字符串比较

大于 小于 等于 不等于
if [ "$a" \> "$b" ] if [ "$a" \< "$b" ] if [ "$a" = "$b" ]
if [ "$a" == "$b" ]
if [ "$a" != "$b" ]
if [[ "$a" > "$b" ]] if [[ "$a" < "$b" ]] if [[ "$a" = "$b" ]]
if [[ "$a" == "$b" ]]
if [[ "$a" != "$b" ]]

[[ ]] and []

功能 [[ ]] [ ] 例子
字符串比较 > \>  -
=(or ==) =  -
!= !=  -
表达式组合 && -a [[ -n $var && -f $var ]] && echo “$var is a file”
|| -o  -
模式匹配
(Pattern matching)
=(or ==) 木有 [[ $name = "a*" ]] -> the string “a*”
正则匹配(RegularExpression matching) =~ 木有 [[ $(date) =~ ^Fri\ ...\ 13 ]] && echo “It’s Friday the 13th!”

特性

例子

文件或文件夹存在

-e

[[ -e $config ]] && echo ”config file exists: $config”

文件新旧比较

-nt/-ot

[[ $file0 -nt $file1 ]] && echo ”$file0 is newer than $file1″

同一个文件

-ef

[[ $input -ef $output ]] \ && { echo ”will not overwrite input file: $input”; exit 1; }

否定

!

-

逻辑组合判断的一些例子:
if [ $condition1 ] && [ $condition2 ]
if [ $condition1 -a $condition2 ]
if [[ $condition1 && $condition2 ]]
if [ $condition1 ] || [ $condition2 ]
if [ $condition1 -o $condition2 ]
if [[ $condition1 || $condition2 ]] # Also works.
#The &&, ||, operators work within a [[ ]] test, despite giving an error within a [ ] construct.

bash shell test条件测试[[ ]]和[ ]异同小结相关推荐

  1. 【Shell】shell编程条件测试if,for,case,select,while

    shell编程条件测试 Shell条件判断初始 Shell分支if语句 Shell循环for语句 Shell分支case语句 Select列表select循环 Shell 循环控制continue,b ...

  2. [zz]shell 中条件测试

    写脚本时,经常要判断字符串是否相等,检查文件状态或是数字测试等.Shell提供了对字符串.文件.数值及逻辑操作等内容的条件测试的支持. 1.测试文件状态 test一般有两种格式,即: test con ...

  3. shell脚本条件测试、正整数字符串比较与if、case语句

    目录 条件测试 三种测试方法 选项 比较整数数值 字符串比较 脚本中常用命令 echo命令 date命令 cal命令 tr命令 cut命令 sort命令 uniq命令 cat多行重定向 if语句 分支 ...

  4. BASH SHELL 脚本基础

    什么是shell     Linux系统的shell作为操作系统的外壳,为用户提供使用操作系统的接口.它是命令语言.命令解释程序及程序设计语言的统称. shell是用户和Linux内核之间的接口程序, ...

  5. linux系统下的bash shell指令有哪些?对bash命令的汇总和小结

    一.参考来源 Windows系统的cmd命令哪些?具体怎么用?cmd命令汇总,dos命令小结?和linux命令的对比? 下面这个网站,强烈推荐,一旦遇到自己不懂的linux命令,直接去这个开源站检索即 ...

  6. shell脚本中的特殊变量与if条件测试

    1.特殊变量 实际工作中我们不可避免的遇到一些xxxx.sh脚本文件,实际阅读shell脚本代码时经常会遇到很多特殊变量(例如:$0.$n.$#.$@.$*.$?.$$等),我们常常会被这些特殊符号折 ...

  7. bash脚本编程之条件判断、条件测试

    脚本编程: 编程面向过程有如下几种执行方式 顺序执行 选择执行:  如 if, case 循环执行:  如 for, while, until bash的变量类型: 本地变量 set VAR_NAME ...

  8. SHELL编程之条件测试

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 一.条件测试(1) 1.test测试操作 2.字符串比较 3.整数值比较 4.文本状态测试 二.条件测试(2) 1.控制操作 ...

  9. Shell脚本基础 、 使用变量 、 条件测试及选择 、 列表式循环案例

    Shell脚本基础 . 使用变量 . 条件测试及选择 . 列表式循环案例 1 案例1:Shell脚本的编写及测试 1.1 问题 1.2 方案 1.3 步骤 2 案例2:重定向输出的应用 2.1 问题 ...

最新文章

  1. AI 在携程智能客服的应用
  2. hadoop slaves文件_hadoop:分布式集群参数master节点的配置!
  3. iOS基础-UIKit框架-高级视图-UIDatePicker
  4. (数据结构与算法)数组模拟队列和环形队列
  5. 『设计模式』JAVA I/O 与装饰者模式UML图
  6. 二十四点游戏python_[求助]关于二十四点游戏python
  7. leetcode 1184 python
  8. 大脑遗忘与数据结构中的对列相似
  9. 各浏览器中的 WebRTC 表现对比【转】
  10. 基于PHP的教学管理系统_WEB管理系统_MySQL应用
  11. 2020年的成长印记
  12. 爱和感恩2021年末记
  13. 7-20 | 打印九九口诀表
  14. 计算机表格中的及格率怎么做,【excle表格不及格百分比】如何使用Excel计算优秀、良好、合格、不合格的比例?...
  15. 洛谷P5071 [YNOI2015]此时此刻的光辉 莫队+玄学优化+卡常QWQ
  16. Runnable 和 Callable 的区别
  17. RH850从0搭建Autosar开发环境【3】- Davinci Configurator之MCU模块配置详解
  18. 如何测试视频会议的延迟
  19. python线程和c++线程的区别_Python、线程、吉尔和C++
  20. stm32H7 SPI和SPI DMA时间差异对比

热门文章

  1. My story with XJTLU Library
  2. 剑桥MPhil Industrial Systems, Manufacture and Management录取率
  3. 回学校之前在家的清理计划
  4. 多智能系统的第一个小视频
  5. C#new出来的结构体内存分配在堆上
  6. 【转】Java虚拟机(JVM)以及跨平台原理
  7. Hud 敌兵布阵 --线段树的插点问线
  8. 博弈入门(思想)HDkiki‘s game;
  9. oracle 表空间
  10. Upma Xmac 测试 03