I need to pass 1 value between programs. In my case, I run (VERY SIMPLE) program within another by calling system("SimpleProgram").
我需要在程序之间传递1个值。在我的情况下,我通过调用系统(“SimpleProgram”)在另一个程序中运行(非常简单)程序。
Is there a way how to pass 1 value (integer) returned by SimpleProgram. Neither "return 123" nor "exit(123)" doesnt work.
有没有办法如何传递SimpleProgram返回的1个值(整数)。 “返回123”和“退出(123)”都不起作用。
Is there any elegant way to pass such value? (I dont want to write and read an external file)
是否有任何优雅的方式来传递这样的价值? (我不想写和读外部文件)
EDIT:
The language is C++, the programming is done on BeagleBone with Angstrom distribution.
语言是C ++,编程是在带有Angstrom发行版的BeagleBone上完成的。
retCode = system("cd /home/martin/uart/temp/xml_parser && ./xmldom");
retCode = system(“cd / home / martin / uart / temp / xml_parser && ./xmldom”);
2 个解决方案
#1
Note what the man page for system(3)
says about the return code:
请注意系统(3)的手册页说明返回代码:
The value returned is -1 on error (e.g. fork(2) failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status).
错误时返回的值为-1(例如fork(2)失败),否则返回命令的返回状态。后一种返回状态采用wait(2)中指定的格式。因此,命令的退出代码将是WEXITSTATUS(状态)。
So you're almost there. If you have a simple program that returns 123, as you stated:
所以你几乎就在那里。如果你有一个简单的程序返回123,如你所说:
int main(int argc, char **argv) {
return 123;
}
then you can run it with system(3)
and see its return code by using WEXITSTATUS()
:
然后你可以用system(3)运行它并使用WEXITSTATUS()查看它的返回码:
#include <iostream>
using namespace std;
#include <stdlib.h>
int main(int argc, char **argv) {
int rc = system(argv[1]);
cout << WEXITSTATUS(rc) << '\n';
}
Naming the first program return123
and the second system
:
命名第一个程序return123和第二个系统:
$ ./system ./return123
123
If you leave off the WEXITSTATUS()
and just print rc
directly, you will get an incorrect value.
如果您不使用WEXITSTATUS()并直接打印rc,则会得到不正确的值。
#2
The standard way to do this is with UNIX pipes.
执行此操作的标准方法是使用UNIX管道。
If it's just a hack, you might as well just use the binary return value, but in either case, you'd have to use execve() instead of system().
如果它只是一个hack,你也可以只使用二进制返回值,但在任何一种情况下,你都必须使用execve()而不是system()。
#1
Note what the man page for system(3)
says about the return code:
请注意系统(3)的手册页说明返回代码:
The value returned is -1 on error (e.g. fork(2) failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status).
错误时返回的值为-1(例如fork(2)失败),否则返回命令的返回状态。后一种返回状态采用wait(2)中指定的格式。因此,命令的退出代码将是WEXITSTATUS(状态)。
So you're almost there. If you have a simple program that returns 123, as you stated:
所以你几乎就在那里。如果你有一个简单的程序返回123,如你所说:
int main(int argc, char **argv) {
return 123;
}
then you can run it with system(3)
and see its return code by using WEXITSTATUS()
:
然后你可以用system(3)运行它并使用WEXITSTATUS()查看它的返回码:
#include <iostream>
using namespace std;
#include <stdlib.h>
int main(int argc, char **argv) {
int rc = system(argv[1]);
cout << WEXITSTATUS(rc) << '\n';
}
Naming the first program return123
and the second system
:
命名第一个程序return123和第二个系统:
$ ./system ./return123
123
If you leave off the WEXITSTATUS()
and just print rc
directly, you will get an incorrect value.
如果您不使用WEXITSTATUS()并直接打印rc,则会得到不正确的值。
#2
The standard way to do this is with UNIX pipes.
执行此操作的标准方法是使用UNIX管道。
If it's just a hack, you might as well just use the binary return value, but in either case, you'd have to use execve() instead of system().
如果它只是一个hack,你也可以只使用二进制返回值,但在任何一种情况下,你都必须使用execve()而不是system()。