当前位置:网站首页>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 脚本可读性更强,后期维护起来也更方便。
扫描下面的二维码关注我的微信公众号:

边栏推荐
- Pan Aimin, chairman of instruction set, attended the 2022 ecug con to speak for China's technical forces
- Jmeter: interface automation test - BeanShell compares database data and return data
- ?实验 7 基于 Mysql 的 PHP 管理系统实现
- Digital image processing Chapter 1 Introduction
- 想sink 到 redis-hash 里面 把 对象的属性和值都写进去 ,大佬们有Demo 吗?
- PHP defines the array using commas,
- Music website management system based on SSM
- Consideration on how the covariance of Kalman filter affects the tracking effect of deepsort
- 美联储SR 11-7:模型风险管理指南(Guidance on Model Risk Management)-万字收藏
- 优炫数据库主要线程有哪些?
猜你喜欢

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

MySQL2

整体二分?

Instruction set x digital technology accelerates the digital transformation of government and enterprises, and builds Unicorn enterprise alliance in DT field

Analysis of strong tennis cup 2021 PWN competition -- babypwn

零号培训平台课程-1、SQL注入基础

Esp8266 (esp-12f) third party library use -- sparkfun_ Apds9960 (gesture recognition)

Relevant principles of MySQL index optimization

How to submit C4d animation to cloud rendering farm for fast rendering?

Watermelon book chapter 3 - linear model learning notes
随机推荐
pytorch笔记:TD3
MySQL limit paging query optimization practice
LogCat工具
35. Search insert position
Go obtains the processing results of goroutine through channel
想sink 到 redis-hash 里面 把 对象的属性和值都写进去 ,大佬们有Demo 吗?
用typescript实现排序-递增
“蔚来杯“2022牛客暑期多校训练营1
String类的用法
腾讯云服务器SSH链接自动断开解决方法
MySQL quickly compares database table data
How to learn C language? This article gives you the complete answer
MySQL query operation index optimization practice
Convert Excel to csv/csv UTF-8
Jest single test style problem [identity obj proxy] NPM package
Pytorch notes: td3
请教大佬们一个问题,pgsqlcdc任务运行一段时间就不能监测变化了,重启就可以了,这个该从哪方面入
Gbase 8C product introduction
指令集董事长潘爱民出席2022 ECUG Con,为中国技术力量发声
高级IO提纲