我试图使用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;
}
我基本上希望在shell中打印所有one
,two
和three
。我得到以下输出
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;
}