bash的if语句用法之:基本用法

从一个问题引发

最近我们项目碰到一个regression bug究其原因是因为对bash语言语句的理解不深刻。我们有一个脚本接收一个数字参数,指定操作的类型。

#!/bin/bash

if [ $1 -eq 1 ]; then
    echo "do_something_1"
else
    echo "do_other"
fi

后来随着参数类型越来越多,使用数字不是很方便描述,就准备增强脚本使用有意义的字符串参数来表达参数类型:

#!/bin/bash

if [ $1 = "read" ]; then
    echo "do_read"
elif [ $1 = "write" ]; then
    echo "do_write"
else
    echo "do_other"
fi

这两个脚本本身都没有问题;但是考虑到项目的实际情况,很多原来的调用脚本已经使用数字作为参数了,就不想去改那些脚本,就考虑把被调用脚本增强成兼容数字和字符串参数:

#!/bin/bash

if [ $1 -eq 1 -o  $1 = "read" ]; then
    echo "do_read"
elif [ $1 -eq 2 -o $1 = "write" ]; then
    echo "do_write"
else
    echo "do_other"
fi

这个脚本看起来逻辑上是通的,但实际上是不正确的,看测试例子:

$ ./test.sh 1
do_read
$ ./test.sh "read"
./test.sh: line 3: [: read: integer expression expected
./test.sh: line 5: [: read: integer expression expected
do_other

当传入参数是数字的时候,没有问题,当出入字符串的时候,我们看到bash希望报错,提示希望输入一个数字。
这根源上说属于弱类型的解释型语言无法避免的错误,如果是强类型的编译型语言,这个问题就能在编译时刻被发现,不至于在运行时刻才暴露出问题。

解决办法

我们知道eq的比较是数字类型比较,如果我们把数字也按字符串来比较,问题就能轻易避免

#!/bin/bash

if [ $1 = 1 -o  $1 = "read" ]; then
    echo "do_read"
elif [ $1 = 2 -o $1 = "write" ]; then
    echo "do_write"
else
    echo "do_other"
fi

在看运行结果

$ ./test.sh 1
do_read
$ ./test.sh read
do_read
$ ./test.sh 2
do_write
$ ./test.sh write
do_write
$ ./test.sh 3
do_other
$ ./test.sh any
do_other

因为在此场景下数字1,2,3都是按字符串"1","2","3"类进行比较的,也就是说调用者传入的都是字符串。

附件: bash的if语句语法

if [ conditional-expression1 ]; then
    statement...
elif [ conditional-expression2 ]; then
    statement...
else
    statement...
fi

注意关键字then可以换行起,但我个人还是喜欢放在同一行。

条件表达式 conditional-expression

-n STRING               The length of STRING is greater than zero.
-z STRING               The lengh of STRING is zero (ie it is empty).
STRING1 = STRING2       STRING1 is equal to STRING2
STRING1 != STRING2      STRING1 is not equal to STRING2
INTEGER1 -eq INTEGER2   INTEGER1 is numerically equal to INTEGER2
INTEGER1 -gt INTEGER2   INTEGER1 is numerically greater than INTEGER2
INTEGER1 -lt INTEGER2   INTEGER1 is numerically less than INTEGER2
-d FILE                 FILE exists and is a directory.
-e FILE                 FILE exists.
-r FILE                 FILE exists and the read permission is granted.
-s FILE                 FILE exists and it's size is greater than zero (ie. it is not empty).
-w FILE                 FILE exists and the write permission is granted.
-x FILE                 FILE exists and the execute permission is granted.

条件表达式的逻辑运算
与或非操作

[ cond-exper1 -a cond-exper2]  # and
[ cond-exper1 -o cond-exper2]  # or
[ ! cond-exper ]               # not
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 3,265评论 0 4
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,841评论 18 139
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,474评论 9 467
  • 一月。 我们分别。 二月。 我去了厦门。 三月。 我去了庐山。
    苏打绿的水阅读 251评论 0 4
  • 充满感恩的一天,早上医院的查询结果是好的,悬着的心终于落地,感叹健康活着的不易。 上午第一次接触钢琴,手指触在琴弦...
    简兮简兮方将万舞阅读 237评论 0 1