bash 中“exec”和“exit”的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8874596/
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-09-18 01:18:49  来源:igfitidea点击:

difference between "exec" and "exit" in bash

bashexecexit

提问by Martin

I have seen both "exit" and "exec" used in bash script to stop script execution if an error has occurred. For example:

我已经看到 bash 脚本中使用“exit”和“exec”来在发生错误时停止脚本执行。例如:

if [ ! -f file ]; then
echo "no file"
exit 1
fi

and:

和:

if [ ! -f file ]; then
exec echo "no file"
fi

What is the best practise here and why? Wider discussion/explanations regarding "exec" and "exit" are welcome as well :)

这里的最佳实践是什么,为什么?也欢迎对“exec”和“exit”进行更广泛的讨论/解释:)

回答by zwol

exitjust exits the shell, returning the specified numeric exit code (or 0 if omitted) to the parent process. It is equivalent to the C library routine also called exit, or to returning from main.

exit只是退出 shell,将指定的数字退出代码(如果省略则为 0)返回给父进程。它等同于 C 库例程也被调用exit,或从main.

execreplacesthe shell with a new executable image (still running in the same process), which will in due course exit and return some code or other to the parent process. It is roughly equivalent to the C library routine execvp.

exec用一个新的可执行映像(仍在同一进程中运行)替换shell,它将在适当的时候退出并向父进程返回一些代码或其他代码。它大致相当于 C 库例程execvp

Your first example is almost, but not quite, the correct way to stop a script if an error has occurred. It should read

您的第一个示例几乎(但不完全)是发生错误时停止脚本的正确方法。它应该读

if [ ! -f file ]; then
    echo "no file" >&2
    exit 1
fi

The >&2, which is missing from your example, causes the error message to go to stderr, where it belongs, instead of stdout, where it does not belong.

>&2,这是从你的榜样缺失,导致该错误消息去stderr,它属于的地方,而不是stdout,它不属于。

Your second example is wrong. In addition to the error message going to the wrong place, exec echowill stop the script (because /bin/echowill do its thing and then exit) but it will return exit code 0 to the parent process. Exit code 0 means success. Programs running in the Unix environment must always make sure to return a nonzeroexit code when they have failed.

你的第二个例子是错误的。除了错误消息到错误的地方,exec echo将停止脚本(因为/bin/echo会做它的事情然后退出)但它会返回退出代码 0 给父进程。退出代码 0 表示成功。在 Unix 环境中运行的程序必须始终确保在失败时返回非零退出代码。

The proper use of execis in shell scripts that do some set-up work and then invoke a long-lived program written in another language, and don't have anything to do afterward, so there's no point keeping the shell process hanging around. For example:

的正确用法exec是在 shell 脚本中进行一些设置工作,然后调用用另一种语言编写的长期存在的程序,之后没有任何事情可做,因此没有必要让 shell 进程挂起。例如:

#! /bin/sh
PATH=/special/directory/for/foo:$PATH
export PATH
exec foo

回答by Sven Marnach

The command exitexits the current shell with the given exit code. The command execreplaces the current shell by the new process defined by the arguments. This also effectively terminates the script after the process terminates, the exit code being the exit code of the new process.

该命令exit使用给定的退出代码退出当前 shell。该命令exec用参数定义的新进程替换当前 shell。这也有效地在进程终止后终止脚本,退出代码是新进程的退出代码。

The first of your code snippets calls the internal shell command echoand then terminates the shell with exit code 1. The second one replaces the shell by the external program echo, which will probably terminate with exit code 0.

第一个代码片段调用内部 shell 命令echo,然后以退出代码 1 终止 shell。第二个代码片段用外部程序替换 shell echo,它可能会以退出代码 0 终止。

I'd definitely recommend the first variant. It saves launching the external command /bin/echoand correctly indicates an error with the exit code.

我肯定会推荐第一个变体。它保存启动外部命令/bin/echo并正确指示退出代码的错误。

回答by SteveK

The proper way to indicate an error is for the script to return a non-zero code, like 1. In your case, the best way is to use "exit 1".

指示错误的正确方法是让脚本返回一个非零代码,例如 1。在您的情况下,最好的方法是使用“exit 1”。

Using "exec" will simply execute the code after it. Perhaps the examples you've seen are using "#!/bin/bash -e" or "set -e", then using "exec" to call code that fails, thus causing "exec" itself to return a non-zero code, and the "-e" means if any command returns a non-zero code or evaluates to false, exit the script immediately.

使用“exec”将简单地执行它之后的代码。也许您看到的示例是使用“#!/bin/bash -e”或“set -e”,然后使用“exec”调用失败的代码,从而导致“exec”本身返回非零代码,“-e”表示如果任何命令返回非零代码或计算结果为 false,则立即退出脚本。