gpt4 book ai didi

linux - 当涉及到失败状态时,shell 函数可以充当命令吗?

转载 作者:太空宇宙 更新时间:2023-11-04 10:08:07 25 4
gpt4 key购买 nike

对于可能会因非零退出代码而失败的可执行文件,可以这样做:

executable && echo "suceed" || echo "failure"

如何使用 shell 函数做到这一点?

myfunction() {
executable arg1 arg2
}

myfunction && echo "succeed" || echo "failure"

最佳答案

来自 the bash manual :

When executed, the exit status of a function is the exit status of the last command executed in the body.

换句话说,shell 函数的行为与您在问题中所展示的完全一样。例如,给定:

myfunction() {
false
}

运行:

myfunction && echo success || echo failed

结果:

failed

另一方面,如果我们有:

myfunction() {
true
}

运行相同的命令返回成功

关于linux - 当涉及到失败状态时,shell 函数可以充当命令吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50896362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com