【发布时间】:2018-09-28 13:51:04
【问题描述】:
我有一个在 bash 脚本中调用的 python 脚本,如下所示:
python3 CheckRef.py --reference $EXPECTED --result $RESULT.npy
echo "return code = $?"
脚本从 python 脚本打印正确的返回码。例如,如果 python 脚本返回-2,它会打印254。
但是,当我在 c++ 程序中调用 bash 脚本时,返回码是空的。它应该返回-2。
testClass test;
auto result = test.execute(command.c_str());
std::cout << "result is " << result << std::endl;
谁能告诉我我做错了什么?
这是execute()函数
std::string testClass::execute(const char* cmd) {
char buffer[128];
std::string result = "";
FILE* pipe = popen(cmd, "r");
if (!pipe) throw std::runtime_error("popen() failed!");
try {
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}
【问题讨论】:
-
bash 脚本中的最后一个命令是
echo。它的退出状态是0。