Bash脚本:函数使用与常见流程实现
1. 函数中的exit命令使用
在Bash脚本里,函数能够运用 exit 命令。当使用该命令时,这意味着告知Bash立即停止函数当前的操作,并退出函数命令块。此时返回的值是调用 exit 命令前最后执行命令的错误级别。
以下是一个示例代码:
#!/bin/bash
#exiting from a function before function finishes
function never {
echo This function has two statements, one will never be \
printed.
exit
echo This is the message that will never print
}
#here we run the function
never
执行上述脚本后,输出结果如下:
demo@cli1:~/scripting$ bash funcreturn3.sh
This function has two statements, one will never be printed.
从输出可以看出,由于使用了 exit 语句,第二条输出语句并未执行。
使用 exit 命令的主要目的在于更精准地控制函数以及脚本中命令的执行顺序。Bas
Bash脚本函数与流程实现详解
超级会员免费看
订阅专栏 解锁全文
1832

被折叠的 条评论
为什么被折叠?



