当前位置:网站首页>Bash: create a function that returns a Boolean value
Bash: create a function that returns a Boolean value
2022-07-27 07:28:00 【TechForGeek】
Scene description
Writing Shell Script time , Sometimes I will encounter more complex logical judgments , Only when these complex logical judgments return true or false Only when , At this time, if these complex logical judgments are written directly in if Back , It will look messy , Poor readability , And it is not easy to maintain later .
resolvent
For the scenario described above , We can write complex logical judgments into a function , According to the results of these complex logical judgments , We hope this function can return true or false.
however ,Bash There is no Boolean value in , Reference resources bash Of man manual , Function return Only one value can be returned :
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
however , stay Shell in , Whether a command is successfully executed can be used exit code To judge ,0 It means success , Not 0 Value to indicate failure :
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
And in the function return value , In fact, it is the exit state of the function , So we can use return 0 To express true, return 1 To express false.
Example
Here is an example , In this case , We determine whether a given path is a directory :
#!/bin/bash
function isDir() {
path="$1"
if [ -d "$path" ]
then
# 0 Express true
return 0
else
# 1 Express false
return 1
fi
}
path="/tmp"
if isDir "$path"
then
echo "$path is dir"
else
echo "$path is not dir"
fiIn the example above , We wrote the conditional judgment isDir() Function , But when judging that the given path is a directory , return 0 (true); Otherwise return to 1 (false).
as everyone knows ,Linux In the system /tmp Is a directory , Therefore, the running result of the above program is :
/tmp is dirOf course , The above example is relatively simple , There is no need to use functions , But when you encounter scenes with complex logical judgments , It would be a better way to write logical judgments into functions .
summary
Shell There is no boolean type in , But we can use in functions return 0 Said to return to true;return 1 Express false. Writing Shell Script time , Write complex logical judgments into functions , And then by going back 0 or 1 To express true or false, Will make Shell Scripts are more readable , Later maintenance is also more convenient .
Scan the QR code below to follow my wechat official account :

边栏推荐
- How to submit C4d animation to cloud rendering farm for fast rendering?
- Quickly update the information in a field in kettle
- [Vani has a date] tail on rainy days
- 2022 0726 Gu Yujia's study notes
- Analysis of memory structure of C program code
- Tcp/ip protocol analysis (tcp/ip three handshakes & four waves + OSI & TCP / IP model)
- Which C4d cloud rendering platform to cooperate with?
- QT连接sqlite数据库的错误及其修改办法
- C4D动画如何提交云渲染农场快速渲染?
- 多线程【初阶-上篇】
猜你喜欢

Li Mu hands-on learning, in-depth learning, V2 transformer and code implementation

在mac中使用docker来搭建oracle数据库服务器

TCP/IP协议分析(TCP/IP三次握手&四次挥手+OSI&TCP/IP模型)

C# 常用功能整合-3

(2022牛客多校三)A-Ancestor(LCA)

MySQL2

网络入门——vlan及trunk概述

Chapter 6 Shell Logic and Arithmetic

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

Jmeter: interface automation test - BeanShell compares database data and return data
随机推荐
Use reflection to dynamically modify annotation attributes of @excel
(2022牛客多校三)A-Ancestor(LCA)
请问有人使用oracle xstream 时出现个别capture延迟很大的吗,该如何解决延迟问题呢
Using loops to process data in tables in kettle
sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)
js做一个红绿灯
(2022杭电多校三)1011.Taxi(曼哈顿最值+二分)
functools模块
临界区(critical section 每个线程中访问 临界资源 的那段代码)和互斥锁(mutex)的区别(进程间互斥量、共享内存、虚拟地址)
[wsl2] configure the USB camera connecting the USB device and using the host
Analysis of query results using both left join on and where in MySQL
Compiling and using log4cxx in rhel7.3
整体二分?
网络入门——vlan及trunk概述
【QT】无法在QT创建者中打开包含文件pcap.h(C1083)
12. Integer to Roman整数转罗马数字
想sink 到 redis-hash 里面 把 对象的属性和值都写进去 ,大佬们有Demo 吗?
零号培训平台课程-1、SQL注入基础
Single arm routing (explanation + experiment)
JS make a traffic light