为什么我的“退出"命令不能退出我的Bash脚本?

编程入门 行业动态 更新时间:2024-10-28 02:32:00
本文介绍了为什么我的“退出"命令不能退出我的Bash脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个Bash脚本:

I have this Bash script:

#!/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!

我看到命令 exit ,但是它没有退出脚本-为什么 exit 不起作用?

I see the command exit, but it doesn't exit the script – why doesn't exit work?

推荐答案

您正在从子外壳程序内部调用 exit ,因此该外壳程序正在退出.尝试以下方法:

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?" }

但是,它将从名为 callee 的任何shell中退出.您可能要使用 return 而不是 exit 从函数中返回.

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

更多推荐

为什么我的“退出"命令不能退出我的Bash脚本?

本文发布于:2023-07-07 09:09:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1061440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   命令   quot   Bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!