Linux 任何退出bash脚本但不退出终端的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9640660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:04:20  来源:igfitidea点击:

Any way to exit bash script, but not quitting the terminal

linuxbash

提问by Richard

When I use exitcommand in a shell script, the script will terminate the terminal (the prompt). Is there any way to terminate a script and then staying in the terminal?

当我exit在 shell 脚本中使用command 时,脚本将终止终端(提示)。有没有办法终止脚本然后留在终端中?

My script run.shis expected to execute by directly being sourced, or sourced from another script.

我的脚本run.sh应该通过直接获取或从另一个脚本获取来执行。

EDIT: To be more specific, there are two scripts run2.shas

编辑:更具体地说,有两个脚本run2.sh作为

...
. run.sh
echo "place A"
...

and run.shas

并且run.sh作为

...
exit
...

when I run it by . run2.sh, and if it hit exitcodeline in run.sh, I want it to stop to the terminal and stay there. But using exit, the whole terminal gets closed.

当我通过 运行它时. run2.sh,如果它打到exit代码行中run.sh,我希望它停在终端并留在那里。但是使用exit,整个终端都会关闭。

PS: I have tried to use return, but echocodeline will still gets executed....

PS:我试过使用return,但echo代码行仍然会被执行....

采纳答案by Dominik Honnef

The "problem" really is that you're sourcing and not executing the script. When you source a file, its contents will be executed in the current shell, instead of spawning a subshell. So everything, including exit, will affect the current shell.

“问题”实际上是您正在采购而不是执行脚本。当您获取文件时,其内容将在当前 shell 中执行,而不是生成子 shell。所以一切,包括退出,都会影响当前的shell。

Instead of using exit, you will want to use return.

而不是使用exit,你会想要使用return

回答by Teja

1) exit 0 will come out of the script if it is successful.

1) 如果成功,退出 0 将从脚本中出来。

2) exit 1 will come out of the script if it is a failure.

2) 如果失败,退出 1 将退出脚本。

You can try these above two based on ur req.

您可以根据您的要求尝试以上两个。

回答by ruakh

Yes; you can use returninstead of exit. Its main purpose is to return from a shell function, but if you use it within a source-d script, it returns from that script.

是的; 您可以使用return代替exit. 它的主要目的是从 shell 函数返回,但如果您在source-d 脚本中使用它,它会从该脚本返回。

As §4.1 "Bourne Shell Builtins" of the Bash Reference Manualputs it:

正如Bash 参考手册的§4.1“Bourne Shell Builtins”所说:

     return [n]

Cause a shell function to exit with the return value n. If nis not supplied, the return value is the exit status of the last command executed in the function. This may also be used to terminate execution of a script being executed with the .(or source) builtin, returning either nor the exit status of the last command executed within the script as the exit status of the script. Any command associated with the RETURNtrap is executed before execution resumes after the function or script. The return status is non-zero if returnis used outside a function and not during the execution of a script by .or source.

     return [n]

导致 shell 函数以返回值n退出。如果未提供n,则返回值是函数中执行的最后一个命令的退出状态。这也可用于终止使用.(or source) 内置函数执行的脚本的执行,返回n或脚本中执行的最后一个命令的退出状态作为脚本的退出状态。与RETURN陷阱关联的任何命令都会在函数或脚本之后恢复执行之前执行。如果return在函数外部使用而不是在脚本执行期间使用.or ,则返回状态为非零source

回答by Umae

This is just like you put a run function inside your script run2.sh. You use exit code inside run while source your run2.sh file in the bash tty. If the give the run function its power to exit your script and give the run2.sh its power to exit the terminator. Then of cuz the run function has power to exit your teminator.

这就像您在脚本 run2.sh 中放置了一个运行函数一样。您在运行中使用退出代码,同时在 bash tty 中获取 run2.sh 文件。如果赋予 run 函数退出脚本的权力,并赋予 run2.sh 退出终止符的权力。然后因为 run 函数有能力退出你的终结者。

    #! /bin/sh
    # use . run2.sh

    run()
    {
        echo "this is run"
        #return 0
        exit 0
    }

    echo "this is begin"
    run
    echo "this is end"

Anyway, I approve with Kaz it's a design problem.

无论如何,我同意 Kaz 这是一个设计问题。

回答by technosaurus

if your terminal emulator doesn't have -holdyou can sanitize a sourced script and hold the terminal with:

如果您的终端模拟器没有,-hold您可以清理源脚本并使用以下命令保持终端:

#!/bin/sh
sed "s/exit/return/g" script >/tmp/script
. /tmp/script
read

otherwise you can use $TERM -hold -e script

否则你可以使用 $TERM -hold -e script

回答by JBoy

I think that this happens because you are running it on source mode with the dot

我认为发生这种情况是因为您在带有点的源模式下运行它

. myscript.sh

You should run that in a subshell:

您应该在子shell中运行它:

/full/path/to/script/myscript.sh

'source' http://ss64.com/bash/source.html

'源' http://ss64.com/bash/source.html

回答by coder23

Also make sure to return with expected return value. Else if you use exit when you will encounter an exit it will exit from your base shell since source does not create another process (instance).

还要确保以预期的返回值返回。否则,如果您在遇到退出时使用 exit,它将从您的基本 shell 中退出,因为 source 不会创建另一个进程(实例)。

回答by Viorel Mirea

Instead of running the code using . run2.shyou can run the script using sh run2.shor bash run2.sh
A new instance will be opened to run the script then it will be closed at the end of the script leaving the other shell opened. `

. run2.sh您可以使用sh run2.sh或运行脚本而不是运行代码bash run2.sh
将打开一个新实例来运行脚本,然后它将在脚本结束时关闭,而让另一个 shell 保持打开状态。`

回答by Beejor

It's correct that sourced vs. executed scripts use returnvs. exitto keep the same session open, as others have noted.

正如其他人所指出的那样,源脚本与执行脚本使用returnvs.exit来保持相同的会话打开是正确的。

Here's a related tip, if you ever want a script that should keep the session open, regardless of whether or not it's sourced.

这是一个相关的提示,如果您想要一个应该保持会话打开的脚本,无论它是否来源。

The following example can be run directly like foo.shor sourced like . foo.sh/source foo.sh. Either way it will keep the session open after "exiting". The $@string is passed so that the function has access to the outer script's arguments.

以下示例可以foo.sh. foo.sh/一样直接运行或来源source foo.sh。无论哪种方式,它都会在“退出”后保持会话打开。该$@字符串传递,这样函数可以访问外部脚本的参数。

#!/bin/sh
foo(){
    read -p "Would you like to XYZ? (Y/N): " response;
    [ $response != 'y' ] && return 1;
    echo "XYZ complete (args $@).";
    return 0;
    echo "This line will never execute.";
}
foo "$@";

Terminal result:

最终结果:

$ foo.sh
$ Would you like to XYZ? (Y/N): n
$ . foo.sh
$ Would you like to XYZ? (Y/N): n
$ |
(terminal window stays open and accepts additional input)

$ foo.sh
$ 你想 XYZ 吗?(是/否):n
$ 。foo.sh
$ 你想 XYZ 吗?(是/否): n
$ |
(终端窗口保持打开并接受额外的输入)

This can be useful for quickly testing script changes in a single terminal while keeping a bunch of scrap code underneath the main exit/returnwhile you work. It could also make code more portable in a sense (if you have tons of scripts that may or may not be called in different ways), though it's much less clunky to just use returnand exitwhere appropriate.

这对于在单个终端中快速测试脚本更改很有用,同时在您工作时在主exit/下保留一堆废代码return。它还可以使代码在一定意义上更轻便的(如果你有吨,可能会或可能不会被以不同的方式被称为脚本),虽然它更笨重只使用returnexit适当的地方。

回答by Tzunghsing David Wong

To write a script that is bulletproof to be run as either a shell script or been sourced as an rc file, the script can check and compare $0and $BASH_SOURCEand determine if exitcan be safely used.

要编写一个脚本,是防弹要运行无论是作为shell脚本或已经来源为RC文件,脚本可以检查和比较$0,并$BASH_SOURCE和确定是否exit可以安全使用。

Here is short code snippet for this

这是用于此的简短代码片段

[ "X$(basename ##代码##)" = "X$(basename $BASH_SOURCE)" ] && \
    echo "***** executing $name_src as a shell script *****" || \
    echo "..... sourcing $name_src ....."