$ 的含义? shell 脚本中的(美元问号)

发布于 2024-12-02 04:34:07 字数 65 浏览 0 评论 0原文

是什么?

echo $?

shell编程中的意思

What does

echo $?

mean in shell programming?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

人间不值得 2024-12-09 04:34:07

这是最后执行的命令的退出状态。

,命令 true 始终返回状态 0,而 false 始终返回状态 1

true
echo $? # echoes 0
false
echo $? # echoes 1

例如 手册:(可以通过在 shell 中调用 man bash 来访问)

?     扩展到最近执行的前台管道的退出状态。

按照惯例,退出状态 0 表示成功,非零返回状态表示失败。 在 wikipedia 上了解有关退出状态的更多信息。

还有其他类似的特殊变量,您可以在本在线手册中看到: https://www.gnu.org/s/bash/manual/bash.html#Special-Parameters

This is the exit status of the last executed command.

For example the command true always returns a status of 0 and false always returns a status of 1:

true
echo $? # echoes 0
false
echo $? # echoes 1

From the manual: (acessible by calling man bash in your shell)

?       Expands to the exit status of the most recently executed foreground pipeline.

By convention an exit status of 0 means success, and non-zero return status means failure. Learn more about exit statuses on wikipedia.

There are other special variables like this, as you can see on this online manual: https://www.gnu.org/s/bash/manual/bash.html#Special-Parameters

反目相谮 2024-12-09 04:34:07

$? 返回最后执行的命令的退出值。 echo $? 在控制台上打印该值。零表示执行成功,而非零值则映射到各种失败原因。

因此在编写脚本时;我倾向于使用以下语法

if [ $? -eq 0 ]; then
 # do something
else
 # do something else
fi

比较是在等于0或不等于0时进行的。

** 基于评论更新:理想情况下,您不应该使用上面的代码块进行比较,请参阅@tripleee评论和解释。

$? returns the exit value of the last executed command. echo $? prints that value on console. zero implies a successful execution while non-zero values are mapped to various reason for failure.

Hence when scripting; I tend to use the following syntax

if [ $? -eq 0 ]; then
 # do something
else
 # do something else
fi

The comparison is to be done on equals to 0 or not equals 0.

** Update Based on the comment: Ideally, you should not use the above code block for comparison, refer to @tripleee comments and explanation.

泪是无色的血 2024-12-09 04:34:07

echo $? - 给出最近执行的命令的退出状态。此退出状态很可能是一个数字,其中零表示成功,任何非零值表示失败

- 这是 bash 中的一个特殊参数/变量。

$? - 它给出存储在变量“?”中的值。

BASH 中一些类似的特殊参数有 1,2,*,#(通常在 echo 命令中看到为 $1 ,$2 , $* , $# 等)。

echo $? - Gives the EXIT STATUS of the most recently executed command . This EXIT STATUS would most probably be a number with ZERO implying Success and any NON-ZERO value indicating Failure

? - This is one special parameter/variable in bash.

$? - It gives the value stored in the variable "?".

Some similar special parameters in BASH are 1,2,*,# ( Normally seen in echo command as $1 ,$2 , $* , $# , etc., ) .

-柠檬树下少年和吉他 2024-12-09 04:34:07

它具有命令的最后一个状态代码(退出值)。

It has the last status code (exit value) of a command.

别忘他 2024-12-09 04:34:07

最小 POSIX C 退出状态示例

要理解 $?,您必须首先了解定义的进程退出状态的概念 POSIX。在 Linux 中:

  • 当进程调用 exit 系统调用时,即使在进程终止后,内核也会存储传递给系统调用的值(int)。< /p>

    退出系统调用由 exit() ANSI C 函数调用,并在您从 main return 时间接调用。

  • 调用退出子进程的进程 (Bash),通常使用 fork + exec,可以使用 wait< 检索子进程的退出状态/code> 系统调用


考虑 Bash 代码:

$ false
$ echo $?
1

C “等效”是:

false.c

#include <stdlib.h> /* exit */

int main(void) {
    exit(1);
}

bash.c

#include <unistd.h> /* execl */
#include <stdlib.h> /* fork */
#include <sys/wait.h> /* wait, WEXITSTATUS */
#include <stdio.h> /* printf */

int main(void) {
    if (fork() == 0) {
        /* Call false. */
        execl("./false", "./false", (char *)NULL);
    }
    int status;
    /* Wait for a child to finish. */
    wait(&status);
    /* Status encodes multiple fields,
     * we need WEXITSTATUS to get the exit status:
     * http://stackoverflow.com/questions/3659616/returning-exit-code-from-child
     **/
    printf("$? = %d\n", WEXITSTATUS(status));
}

编译并运行:

g++ -ggdb3 -O0 -std=c++11 -Wall -Wextra -pedantic -o bash bash.c
g++ -ggdb3 -O0 -std=c++11 -Wall -Wextra -pedantic -o false false.c
./bash

输出:

$? = 1

在 Bash 中,当您按 Enter 时,会像上面一样发生 fork + exec + wait,然后 bash 设置 $? 到分叉进程的退出状态。

注意:对于像 echo 这样的内置命令,不需要生成进程,Bash 只需将 $? 设置为 0 即可模拟外部进程。

标准和文档

POSIX 7 2.5.2“特殊参数”http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_02

?扩展到最近管道的十进制退出状态(请参阅管道)。

man bash“特殊参数”:

shell 对几个参数进行了特殊处理。这些参数仅供参考;不允许向他们分配。 [...]

?扩展到最近执行的前台管道的退出状态。

ANSI C 和 POSIX 建议:

  • 0 表示程序成功

  • 其他值:程序因某种原因失败。

    确切的值可以指示失败的类型。

    ANSI C 没有定义任何值的含义,POSIX 指定大于 125 的值:“POSIX”是什么意思?

Bash 使用退出状态if

在 Bash 中,我们经常使用退出状态 $? 隐式地控制 if 语句,如下所示:

if true; then
  :
fi

where true 是一个只返回 0 的程序。

上面相当于:

true
result=$?
if [ $result = 0 ]; then
  :
fi

And in:

if [ 1 = 1 ]; then
  :
fi

[ 只是一个具有奇怪名称的程序(Bash 内置程序的行为与此类似),并且1 = 1 ] 其参数,另请参阅: 单和双括号之间的区别Bash 中的双方括号

Minimal POSIX C exit status example

To understand $?, you must first understand the concept of process exit status which is defined by POSIX. In Linux:

  • when a process calls the exit system call, the kernel stores the value passed to the system call (an int) even after the process dies.

    The exit system call is called by the exit() ANSI C function, and indirectly when you do return from main.

  • the process that called the exiting child process (Bash), often with fork + exec, can retrieve the exit status of the child with the wait system call

Consider the Bash code:

$ false
$ echo $?
1

The C "equivalent" is:

false.c

#include <stdlib.h> /* exit */

int main(void) {
    exit(1);
}

bash.c

#include <unistd.h> /* execl */
#include <stdlib.h> /* fork */
#include <sys/wait.h> /* wait, WEXITSTATUS */
#include <stdio.h> /* printf */

int main(void) {
    if (fork() == 0) {
        /* Call false. */
        execl("./false", "./false", (char *)NULL);
    }
    int status;
    /* Wait for a child to finish. */
    wait(&status);
    /* Status encodes multiple fields,
     * we need WEXITSTATUS to get the exit status:
     * http://stackoverflow.com/questions/3659616/returning-exit-code-from-child
     **/
    printf("$? = %d\n", WEXITSTATUS(status));
}

Compile and run:

g++ -ggdb3 -O0 -std=c++11 -Wall -Wextra -pedantic -o bash bash.c
g++ -ggdb3 -O0 -std=c++11 -Wall -Wextra -pedantic -o false false.c
./bash

Output:

$? = 1

In Bash, when you hit enter, a fork + exec + wait happens like above, and bash then sets $? to the exit status of the forked process.

Note: for built-in commands like echo, a process need not be spawned, and Bash just sets $? to 0 to simulate an external process.

Standards and documentation

POSIX 7 2.5.2 "Special Parameters" http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_02 :

? Expands to the decimal exit status of the most recent pipeline (see Pipelines).

man bash "Special Parameters":

The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed. [...]

? Expands to the exit status of the most recently executed foreground pipeline.

ANSI C and POSIX then recommend that:

  • 0 means the program was successful

  • other values: the program failed somehow.

    The exact value could indicate the type of failure.

    ANSI C does not define the meaning of any vaues, and POSIX specifies values larger than 125: What is the meaning of "POSIX"?

Bash uses exit status for if

In Bash, we often use the exit status $? implicitly to control if statements as in:

if true; then
  :
fi

where true is a program that just returns 0.

The above is equivalent to:

true
result=$?
if [ $result = 0 ]; then
  :
fi

And in:

if [ 1 = 1 ]; then
  :
fi

[ is just an program with a weird name (and Bash built-in that behaves like it), and 1 = 1 ] its arguments, see also: Difference between single and double square brackets in Bash

内心激荡 2024-12-09 04:34:07

来自 http://www.gnu.org/s/bash/手册/bash.html#特殊参数

?
Expands to the exit status of the most recently executed foreground pipeline. 

From http://www.gnu.org/s/bash/manual/bash.html#Special-Parameters

?
Expands to the exit status of the most recently executed foreground pipeline. 
用心笑 2024-12-09 04:34:07

请参阅 Bash 手册 www.gnu.org/s/bash/manual/bash.html#Special-Parameters" rel="nofollow noreferrer">3.4.2 特殊参数:

? - 扩展到最近执行的前台管道的退出状态。

它有点难以找到,因为它没有列为 $? (变量名称“只是”?)。当然,另请参阅 退出状态 部分; -)

快乐编码。

See The Bash Manual under 3.4.2 Special Parameters:

? - Expands to the exit status of the most recently executed foreground pipeline.

It is a little hard to find because it is not listed as $? (the variable name is "just" ?). Also see the exit status section, of course ;-)

Happy coding.

雾里花 2024-12-09 04:34:07

输出最后执行的unix命令的结果

0 implies true
1 implies false

Outputs the result of the last executed unix command

0 implies true
1 implies false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文