出口不在bash工作 [英] exit doesn't work in bash

查看:135
本文介绍了出口不在bash工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是bash脚本

#!/bin/bash
set -x
function doSomething() {
    callee
    echo "It should not go to here!"
}

function callee() {
    ( echo "before" ) && (echo "This is callee" && exit 1 )                                                                                   
    echo "why I can see this?"
}


doSomething

和这是结果...

+ set -x
+ doSomething
+ callee
+ echo before
before
+ echo 'This is callee'
This is callee
+ exit 1
+ echo 'why I can see this?'
why I can see this?
+ echo 'It should not go to here!'
It should not go to here!

我看到了命令退出,但它并没有退出退出脚本

I saw the command "exit", but it didn't exit exit the script

为什么退出不工作?

推荐答案

您在呼唤退出从子shell里面,所以这是退出外壳。试试这个:

You are calling exit from inside a subshell, so that's the shell that is exiting. Try this instead:

function callee() {
    ( echo "before" ) && { echo "This is callee" && exit 1; }                                                                                   
    echo "why I can see this?"
}

然而,这将从一个名为任何壳退出被叫方。您可能需要使用收益而不是退出从函数返回。

This, however, will exit from whatever shell called callee. You may want to use return instead of exit to return from the function.

这篇关于出口不在bash工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆