简体   繁体   English

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

[英]Get the return code of a C program in my shell program

Suppose I have a C program named Foo.c which is printing few things and returning a value named rc which I am executing in my shell program as follows : 假设我有一个名为Foo.c的C程序,它打印一些东西并返回一个名为rc的值,我在shell程序中执行如下:

foobar=$(Foo | tail -1)

Now, the variable foobar has the last printed value of the program Foo . 现在,变量foobar具有程序Foo的最后打印值。 But without disturbing this, I want to get the return code rc of the program in my shell program. 但是在不打扰这个的情况下,我想在shell程序中获取程序的返回码rc

If you are using bash shell, you can use PIPESTATUS array variable to get the status of the pipe process. 如果您使用的是bash shell,则可以使用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 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). 一个数组变量,包含最近执行的前台管道(可能只包含一个命令)中的进程的退出状态值列表。

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. 或者另一种选择是使用pipestatus环境变量。 You can read about it here. 你可以在这里读到它。 http://www.linuxnix.com/2011/03/pipestatus-internal-variable.html http://www.linuxnix.com/2011/03/pipestatus-internal-variable.html

This assigns the last line of the output of Foo to foobar and Foo's exit code is assigned to code : 这将Foo输出的最后一行分配给foobar并将Foo的退出代码分配给code

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

The <(...) construct is called process substitution . <(...)构造称为进程替换 In the code above, the read commands receive their stdin from the process substitution. 在上面的代码中, read命令从进程替换中接收stdin。 Because of the tail -2 , the process substitution produces a total of two lines. 由于tail -2 ,过程替换产生总共两条线。 The first line is the last line produced by Foo and it is assigned to foobar . 第一行是Foo生成的最后一行,它被分配给foobar The second is assigned to code . 第二个分配给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

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. 我担心您必须使用临时文件来存储Foo程序的输出,获取返回代码然后执行tail -1。 Just like the following: 如下所示:

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

$? gives the return value of the last executed command. 给出上次执行的命令的返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM