当前位置:网站首页>Bash: 创建返回布尔类型值的函数
Bash: 创建返回布尔类型值的函数
2022-07-27 06:10:00 【TechForGeek】
场景描述
在写 Shell 脚本的时候,有时候会遇到比较复杂的逻辑判断,只有当这些的复杂的逻辑判断返回 true 或 false 时才执行某些操作,这时如果把这些复杂的逻辑判断直接写在 if 后面,会显得比较乱,可读性很差,而且后期不容易维护。
解决方法
对于上面描述的场景,我们可以把复杂的逻辑判断写到一个函数里,根据这些复杂逻辑判断的结果,我们希望这个函数能够返回 true 或 false。
但是,Bash 中并没有布尔类型的值,参考 bash 的 man 手册,函数中的 return 只能返回一个数值:
return [n]
...
The return status is non-zero if return is supplied a non-numeric argument, or is used outside a function and not during execution of a script by . or source.
...
man bash
不过,在 Shell 中,一个命令执行是否成功可以用它的 exit code 来判断,0 表示成功, 非 0 值来表示失败:
For the shell's purposes, a command which exits with a zero exit status has succeeded. An exit status of zero indicates success. A non-zero exit status indicates failure.
man bash
而函数中的 return 值,其实就是函数的退出状态,因此我们可以用 return 0 来表示 true, return 1 来表示 false。
示例
下面通过一个例子来说明,在这个例子中,我们判断一个给定的路径是否是一个目录:
#!/bin/bash
function isDir() {
path="$1"
if [ -d "$path" ]
then
# 0 表示 true
return 0
else
# 1 表示 false
return 1
fi
}
path="/tmp"
if isDir "$path"
then
echo "$path is dir"
else
echo "$path is not dir"
fi在上面的例子中,我们将条件判断写到了 isDir() 函数中,但判断给定路径是目录时,返回 0 (true);否则返回 1 (false)。
众所周知,Linux 系统中 /tmp 是一个目录,因此上面程序的运行结果为:
/tmp is dir当然,上面的例子比较简单,并没有必要使用函数,但是在遇到逻辑判断比较复杂的场景时,将逻辑判断写到函数里会是一个更好的方法。
总结
Shell 中并没有布尔类型,但是我们可以在函数中使用 return 0 表示返回 true;return 1 表示 false。在写 Shell 脚本时,将复杂的逻辑判断写到函数里,然后通过返回 0 或 1 来表示 true 或 false,会让 Shell 脚本可读性更强,后期维护起来也更方便。
扫描下面的二维码关注我的微信公众号:

边栏推荐
- Analysis on the current situation and optimization strategy of customer experience management in banking industry
- 腾讯云服务器SSH链接自动断开解决方法
- C time related operation
- Codeforces Round #809 (Div. 2)(6/6)(Kruskal重构树)
- Firefox browser, when accessing Tencent cloud server, failed to establish a secure connection.
- 使用pip命令切换不同的镜像源
- sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)
- 【golang学习笔记2.0】 golang中的数组和切片
- Neural network parameter initialization
- Gbase 8C - SQL reference 6 SQL syntax (15)
猜你喜欢

泛型 -- 学会它,好处多多

How to learn C language? This article gives you the complete answer

Drools (5): drools basic syntax (3)

No.0 training platform course-2. SSRF Foundation

Which C4d cloud rendering platform to cooperate with?

Watermelon book chapter 3 - linear model learning notes

How to implement Devops with automation tools | including low code and Devops application practice

pytorch笔记:TD3

(posted) comparison of Eureka, consumer and Nacos 2

sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)
随机推荐
How to implement Devops with automation tools | including low code and Devops application practice
Oracle数据库问题
String类的用法
"Weilai Cup" 2022 Niuke summer multi school training camp 1
PHP defines the array using commas,
如何借助自动化工具落地DevOps|含低代码与DevOps应用实践
Quartus:往别人的工程添加.v文件报错
Internal class -- just read this article~
二叉树--天然的查找语义(1)基础篇
用typescript实现排序-递增
C语言程序设计 | 程序编译与预处理
使用反射实现动态修改@Excel的注解属性
Logcat tool
Digital image processing - Chapter 6 color image processing
Neural network parameter initialization
Codeforces Round #804 (Div. 2)(5/5)
Leetcode series (I): buying and selling stocks
Netease Yunxin appeared at the giac global Internet architecture conference to decrypt the practice of the new generation of audio and video architecture in the meta universe scene
How to learn C language? This article gives you the complete answer
How does golang assign values to empty structures