“$?”是什么意思?给我们确切的shell脚本吗?

发布于 2024-12-03 23:22:23 字数 513 浏览 3 评论 0原文

我在网上看到了代码,我想知道“$?”到底是什么意思?做/给我们。 谷歌搜索没有帮助。

这是我在其中看到的代码:

#!/bin/sh

ping -c 2 localhost
if [ $? != 0 ] ; then
    echo "Couldn't ping localhost, weird"
    fi

ping -c 2 veryweirdhostname.noend 
if [ $? != 0 ] ; then
    echo "Surprise, Couldn't ping a very weird hostname.."
    fi

echo "The pid of this process is $$"

取自: http://efod.se/著作/linuxbook/html/shell-scripts.html

I saw the code written somewhere online, and I wanted to know what exactly does "$?" do/give us.
Googling did not help.

Here's the code I saw it in:

#!/bin/sh

ping -c 2 localhost
if [ $? != 0 ] ; then
    echo "Couldn't ping localhost, weird"
    fi

ping -c 2 veryweirdhostname.noend 
if [ $? != 0 ] ; then
    echo "Surprise, Couldn't ping a very weird hostname.."
    fi

echo "The pid of this process is $"

Taken from: http://efod.se/writings/linuxbook/html/shell-scripts.html

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

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

发布评论

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

评论(5

云淡月浅 2024-12-10 23:22:23

$? 是一个变量,保存您运行的最后一个命令的返回值。

C 程序示例 (example.c):

int main() { return 1; }

Bash 示例:

gcc -o example example.c
./example
echo $? # prints 1

$? is a variable holding the return value of the last command you ran.

Example C program (example.c):

int main() { return 1; }

Example Bash:

gcc -o example example.c
./example
echo $? # prints 1
清音悠歌 2024-12-10 23:22:23

大多数答案都缺少一些细节。明确的答案可以在 shell 的 POSIX 标准中有关特殊参数的部分中找到

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

不要对管道这个词感到惊讶,因为即使是像 ls 这样的简单命令在语法上也是由单个命令组成的管道。但是,对于多命令管道来说,$? 是什么?它是管道中最后命令的退出状态。

那么在后台执行的管道又如何呢,比如 grep foo bigfile|head -n 10 >结果&?

一旦管道的最后一个命令完成,就可以通过 wait 检索它们的退出状态。
后台进程 pid 以 $! 形式提供,而 $? 仅报告后台命令是否正确启动

另一个值得一提的细节是退出状态通常在0到255范围内,其中128到255表示进程由于信号而退出。从 C 程序返回其他值可能无法在 $? 中准确反映。

Most of the answers are missing a bit of detail. A definitive answer is found in the POSIX standard for the shell, in the section on special parameters:

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

Don't be surprised by the word pipeline, because even a simple command such as ls is grammatically a pipeline consisting of a single command. But then, what is $? for a multi-command pipeline? It's the exit status of the last command in the pipeline.

And what about pipelines executing in the background, like grep foo bigfile|head -n 10 > result &?

Their exit status can be retrieved through wait once the pipeline's last command has finished.
The background process pid is available as $!, and $? only reports whether the background command was correctly started.

Another detail worth mentioning is that the exit status is usually in the range 0 through 255, with 128 to 255 indicating the process exited due to a signal. Returning other values from a C program is likely to not be reflected accurately in $?.

偏爱自由 2024-12-10 23:22:23

它是最近执行的命令的返回代码。

按照约定,0 表示成功退出,非零表示出现某种错误。

It's the return code from the most recently executed command.

By convention 0 is a successful exit and non-zero indicates some kind of error.

幼儿园老大 2024-12-10 23:22:23

此特殊变量显示在脚本或命令行中运行的最后一个命令的退出状态。例如,在命令行中,用户可以键入

 who; echo $?

输出将为

 user  tty7         2014-07-13 19:47
 0

这显示 who 的输出以及命令的退出状态。脚本也是一样的。

 #!/bin/bash
 who
 echo $?

输出:0

This special variable shows the exit status of the last command that was run in a script or command-line. For example, in a command-line, the user could type

 who; echo $?

The output would then be

 user  tty7         2014-07-13 19:47
 0

This shows the output of who and the exit status of the command. A script would be the same.

 #!/bin/bash
 who
 echo $?

Output: 0

清旖 2024-12-10 23:22:23

其他答案很好地涵盖了 bash,但您没有在问题中指定 shell。在 csh(和 tcsh)中 $? 可用于 查询变量是否存在,eg

if $?my_var then
    echo my_var exists
endif

the other answers cover bash pretty well, but you don't specify a shell in your question. In csh (and tcsh) $? can be used to query the existence of variables, e.g.

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