c - 如何使用数组中的多个参数在 C 中构造 execl() 调用?

标签 c arrays exec

我在 C 中有一个名为 args[] 的字符串数组 - 现在我如何使用这个参数列表来构造对 execl() 的正确调用?

所以如果数组包含:

{"/bin/ls","ls","-a","-l"} 

...我怎样才能最终构建一个 execl() 调用:

execl("/bin/ls","ls","-a","-l",NULL);

我一定是想错了,因为我在网上找不到任何东西,只是谈论定义可以接受可变数量参数的函数。

最佳答案

直接取自“man execl”

The execv() and execvp() functions provide an array of pointers to null-terminated strings that represent the argument list available to the new program. The first argument, by convention, should point to the filename associated with the file being executed. The array of pointers must be terminated by a NULL pointer.

编辑: 这是原型(prototype)。

int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);

关于c - 如何使用数组中的多个参数在 C 中构造 execl() 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2613943/

相关文章:

c - 从管道读取信号发送

c++ - Ubuntu 中的 execl() 参数

python - 获取元组列表中元素元组的最大值

python - 如何遍历 Pandas 数据框中的字符串并删除不需要的单词?

java - 使用 runtime.getRuntime().exec() 时 Jar 文件如何工作

c - 我怎样才能暂停然后恢复对 `sleep` 的调用

c - 错误: expected identifier or ‘(’ before ‘.’ token

arrays - 在 React 中使用 'ref' 作为数组

linux - 如何从 shell 脚本内部更新 shell 脚本

go - 使用 Golang os/exec 执行带有 args 的 ssh 命令会导致退出状态 127