【发布时间】:2018-02-10 16:56:27
【问题描述】:
我正在尝试使用 execlp 返回多行输出,但不确定执行此操作的确切方法。我试过了
#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;
int main()
{
string str = "echo ";
str += "one \n";
str += "two \n";
str += "three \n";
if(execl("/bin/sh","/bin/sh","-c",str.c_str(),(char*)NULL) == -1)
{
printf("Child Execution Failed\n");
exit(1);
}
exit(0);
return 0;
}
我基本上希望所有one、two 和three 都打印在shell 中。我得到以下输出虽然
one
/bin/sh: 2: two: not found
/bin/sh: 3: three: not found
再次感谢!
答案:找到了一个简单的答案:
#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;
int main()
{
string str = "echo one\n";
str += "echo two\n";
str += "echo three\n";
if(execl("/bin/sh","/bin/sh","-c",str.c_str(),(char*)NULL) == -1)
{
printf("Child Execution Failed\n");
exit(1);
}
exit(0);
return 0;
}
【问题讨论】:
-
我正在使用
dup2()将从execlp()收到的信息发送到创建的套接字。你认为这会产生一些问题吗?抱歉不清楚,但我认为这不是问题。