Reputation:
I'd like to use ls from execl and redirect the output to a file, which exists. I tried with this:
int value = execl("/bin/ls","ls","-l",">/home/sbam/myfile",NULL);
But it doesn't work... How can I do?
Thanks.
Upvotes: 0
Views: 238
Reputation: 409356
Redirection is part of the shell and not something the commands handles. Either invoke a shell and execute the command through the shell, or you could open the file using open
and use dup2
to make the file the process standard output.
Something like
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");
Upvotes: 1