我希望使用execl中的ls并将输出重定向到一个存在的文件中。我试过用这个:
int value = execl("/bin/ls","ls","-l",">/home/sbam/myfile",NULL);
但不管用..。我该怎么做?
谢谢。
发布于 2016-01-04 17:31:52
重定向是shell的一部分,而不是命令处理的内容。要么调用shell并通过shell执行命令,要么使用open打开文件并使用dup2使文件成为流程的标准输出。
有点像
int fd = open("/home/sbam/myfile", O_CREAT | O_WRONLY, 0644);
if (fd != -1)
{
if (dup2(fd, STDOUT_FILENO) != -1)
{
if (execl("/bin/ls", "ls", "-l", NULL) == -1)
perror("execl");
}
else
perror("dup2");
}
else
perror("open");https://stackoverflow.com/questions/34596528
复制相似问题