简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM