I’m trying to do the following:
execl("/bin/grep","grep","print",fd,NULL);
where fd is a file descriptor.
So basically this should grep for “print” in the file pointed to by fd.
It doesn’t seem to work although I get no compile errors/warnings.
It works when I give a filename such as "Something.txt" instead of the fd
Can someone tell me why this isn’t working? (I know that execl takes only const char arg* but as I said no compile errors/warnings).
There are 2 issues:
execlinto using a small integer as a pointergrepto understand file descriptorsIf I understand your question correctly, right before you
exec, you should redirect the descriptor intoSTDIN_FILENO. Something like:This should work because
grepanalyzes its stdin when no input files are provided.