简体   繁体   English

使用 exec 在 C 的不同目录中执行 ls

[英]Executing ls in a different directory in C using exec

Title is pretty self-explanatory on what I want to do.标题对我想要做什么是不言自明的。

My current try:我目前的尝试:

  chdir("/Path/I/want/");   //this is different that the path my program is located at
      char * ls_args[] = { "ls" , "ls -l", NULL};
      execv(ls_args[0], ls_args);
    }

I am not getting any errors or output, any help?我没有收到任何错误或 output,有什么帮助吗?

Execv function needs full path of the command which you have to execute. Execv function 需要您必须执行的命令的完整路径。 So, instead of giving "ls" , you should find out where the ls is located in your system by因此,您应该找出 ls 在系统中的位置,而不是给出"ls"

$ which ls

it will probably be in /bin folder.它可能在 /bin 文件夹中。 So you have to give "/bin/ls" to exec.所以你必须把"/bin/ls"给exec。 Also any argument to ls should be another member in array. ls 的任何参数也应该是数组中的另一个成员。 So instead of using所以而不是使用

char * ls_args[] = { "ls", "ls -l", NULL};

use利用

char * ls_args[] = { "/bin/ls", "-l", NULL};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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