-1

当我在 shell 脚本中调用外部应用程序时,我该如何

在可执行应用程序(a.out)中获取 printf 的输出并在 shell 提示符下打印它们?

脚本:

STATUS_CMD="AGQMI stop $PDH $CID"

`$STATUS_CMD` 

clear_state

echo done

AGQMI 是我的应用程序,它有 printf 用于成功和失败的情况,我需要在 shell 输出中看到它们,但是当我运行脚本时我无法查看。

输出:

Clearing state...

done
4

2 回答 2

0

看看这个最近的线程。它的一个示例显示了如何在运行时编译 C 代码。

在这里转发一个总结,基本上概念是这样的。您还可以向其添加更多标志或其他增强功能,例如编译检查。

#!/bin/sh

OUTPUT_BINARY=/tmp/some_binary_name

# Compile the code.

gcc -o "$OUTPUT_BINARY" -xc - <<EOF
#include <stdio.h>

int main(int argc, char** argv)
{
    printf("Hello world.\n");
    return 0;
}
EOF

# Run the code.

"$OUTPUT_BINARY"

# Delete it.

rm -f "$OUTPUT_BINARY"

甚至可能有一些方法可以不使用文件系统文件,只使用空间或管道,但基本上就是这样。

于 2013-09-04T12:32:51.630 回答
0

脚本的标准输出由从脚本运行的程序继承。您可以重定向单个程序,或将它们的输出捕获到变量中。

#!/bin/sh
one_output=$(one -foo "bar" -baz <quux)
echo "the output from 'one' was '$one_output'"
two -pounds "$1" >two_out
echo "the output from 'two' is in the file 'two_out'"
cat two_out
echo "here is the output from 'three'"
three
于 2013-09-04T12:29:57.470 回答