Search code examples
linuxcexec

How can I execute a cat command in a execlp in C?


I'm trying to execute commands that are passed from the terminal to argv seperated by : to be more specific cat nevermind : grep left : wc -c.

tabCommand is an array that contains each command so cat nevermind,grep left,wc -c

With printf I can confirm that tabCommand[i-1] is indead equal to cat nevermind but the output I get is Error: No such file or directory

 if (execl(tabCommande[i-1],tabCommande[i-1], (char *)NULL) == -1) {
        error_and_exit();
 }

If someone can help me find the issue I would really appreciate it.


Solution

  • With the comments I got in my post I managed to find my problem

    execlp("/bin/sh","sh","-c",tabCommande[i], (char *)NULL) works because I need to use the full path. If I do execlp(tabCommande[i],tabCommande[i], (char *)NULL) it won't work because im not using the full path of each command so simply giving cat to execlp won't work.

    found this answer thanks to waltinator I'm new to stack so i dont know how to give you the credit