繁体   English   中英

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

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

标题对我想要做什么是不言自明的。

我目前的尝试:

  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);
    }

我没有收到任何错误或 output,有什么帮助吗?

Execv function 需要您必须执行的命令的完整路径。 因此,您应该找出 ls 在系统中的位置,而不是给出"ls"

$ which ls

它可能在 /bin 文件夹中。 所以你必须把"/bin/ls"给exec。 ls 的任何参数也应该是数组中的另一个成员。 所以而不是使用

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

利用

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

暂无
暂无

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

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