bash 在我的 shell 程序中获取一个 C 程序的返回码

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

Get the return code of a C program in my shell program

cbashshell

提问by molecule

Suppose I have a C program named Foo.cwhich is printing few things and returning a value named rcwhich I am executing in my shell program as follows :

假设我有一个 C 程序Foo.c,它正在打印一些东西并返回一个rc我在 shell 程序中执行的值,如下所示:

foobar=$(Foo | tail -1)

foobar=$(Foo | tail -1)

Now, the variable foobar has the last printed value of the program Foo. But without disturbing this, I want to get the return code rcof the program in my shell program.

现在,变量 foobar 具有程序的最后打印值Foo。但是在不打扰这个的情况下,我想rc在我的shell程序中获取程序的返回码。

采纳答案by Pratik Shinde

You can use "set -o pipefail" option.

您可以使用“set -o pipefail”选项。

[root@myserver Test]# set -o pipefail
[root@myserver Test]# ./a.out | tail -l
[root@myserver Test]# echo $?
100

Here my program a.out returns 100.

这里我的程序 a.out 返回 100。

Or another options is to use pipestatus environment variable. You can read about it here. http://www.linuxnix.com/2011/03/pipestatus-internal-variable.html

或者另一种选择是使用 pipestatus 环境变量。你可以在这里读到它。 http://www.linuxnix.com/2011/03/pipestatus-internal-variable.html

回答by sat

If you are using bashshell, you can use PIPESTATUSarray variable to get the status of the pipeprocess.

如果您使用的是bashshell,则可以使用PIPESTATUS数组变量来获取pipe进程的状态。

$ tail sat | wc -l
tail: cannot open ‘sat' for reading: No such file or directory
0
$ echo "${PIPESTATUS[0]} ${PIPESTATUS[1]}"
1 0
$

From man bash:

来自man bash

PIPESTATUS

An array variable containing a list of exit status values from the processes in the most-recently-executed foreground pipeline (which may contain only a single command).

管道状态

一个数组变量,包含来自最近执行的前台管道(可能只包含一个命令)中的进程的退出状态值列表。

回答by John1024

This assigns the last line of the output of Footo foobarand Foo's exit code is assigned to code:

这将输出的最后一行分配给Footofoobar并将 Foo 的退出代码分配给code

{ read -r foobar; read code; } < <( (Foo; echo $? ) | tail -2)

The <(...)construct is called process substitution. In the code above, the readcommands receive their stdin from the process substitution. Because of the tail -2, the process substitution produces a total of two lines. The first line is the last line produced by Fooand it is assigned to foobar. The second is assigned to code.

<(...)构造称为进程替换。在上面的代码中,read命令从进程替换中接收它们的标准输入。由于tail -2,过程替换总共产生两行。第一行是由 生成的最后一行,Foo并将其分配给foobar。第二个分配给code.

The space between the first and second <is essential.

第一个和第二个之间的空间<是必不可少的。

Example

例子

After creating a function Foo, the above can be tested:

创建一个函数Foo后,可以测试上面的内容:

$ Foo() { echo "Testing"; false; }
$ { read -r foobar; read code; } < <( (echo "Testing"; false; echo $? ) | tail -2)
$ echo "foobar=$foobar code=$code"
foobar=Testing code=1

And:

和:

$ Foo() { echo "2nd Test"; true; }
$ { read -r foobar; read code; } < <( (Foo; echo $? ) | tail -2)
$ echo "foobar=$foobar code=$code"
foobar=2nd Test code=0

回答by rkachach

I'm afraid that you have to use a temporal file to store the output of the Foo program, get the return code and then perform the tail -1. Just like the following:

恐怕你得用一个临时文件来存储Foo程序的输出,得到返回码,然后执行tail -1。就像下面这样:

Foo > /tmp/temp_file
ret=$?
foobar=$(tail -1 /tmp/temp_file)

回答by user3156262

$?gives the return value of the last executed command.

$?给出最后执行的命令的返回值。