0

I know I can do

execl("/bin/ls","-l",(char*)NULL)

to output ls.

But how do I execute

ls | grep -c "file"

using execv? In my code I want to search my current working directory for a particular file name. The above command returns 1 if the file exists and 0 if it doesn't. I want to execute this command from my program using execv. How do I do this?

I tried

execl("/bin/ls", "| grep -c file"  ,(char*)NULL)

But it doesn't work.

Also, even if I execute this command using execl, it will just print the output number in the terminal. How do I get it to return it to the program?

mjsxbo
  • 2,116
  • 3
  • 22
  • 34
  • 1
    Possible duplicate of [Pipes, dup2 and exec()](https://stackoverflow.com/questions/33884291/pipes-dup2-and-exec). Also you might want to look into `system(3)`. – gstukelj Oct 12 '19 at 13:38
  • That code isn't ever implemented as just one execv, unless you're using execv to invoke a shell that sets up pipes, forks, and calls a separate execv for each side of the pipeline. – Charles Duffy Oct 12 '19 at 13:45

0 Answers0