处理 execvp 的参数数组?

发布于 2024-12-26 10:59:47 字数 158 浏览 3 评论 0原文

当我调用 execvp 时,例如 execvp(echo, b) ,其中 b 是命令 a 的参数数组,稍后更改此数组会影响之前进行的 execvp 调用?当我尝试调用 execp(echo, b) 时,它最终打印出 (null) 而不是 b 内部的内容。谁能指出为什么以及我必须做什么才能正确传递参数?

When I call execvp, for example execvp(echo, b) where b is an array of arguments for the command a, will changing this array later affect the execvp call made previously? When I try calling execp(echo, b), it ends up printing out (null) instead of the content inside of b. Can anyone point out why and what I have to do to pass the arguments correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初与友歌 2025-01-02 10:59:47

在调用 exec() 或其相关函数后,您的原始程序将不再存在。这意味着该程序中的任何内容都不会影响 exec() 调用之后的任何内容,因为它永远不会运行。也许您没有正确构建参数数组?以下是 execvp() 的快速工作示例:

#include <unistd.h>

int main(void)
{
  char *execArgs[] = { "echo", "Hello, World!", NULL };
  execvp("echo", execArgs);

  return 0;
}

来自 execvp () 手册页

execv()execvp()execvpe() 函数提供指向空终止字符串的指针数组,这些字符串表示新程序可用的参数列表。按照惯例,第一个参数应该指向与正在执行的文件关联的文件名。指针数组必须NULL指针终止。

一个常见的错误是跳过有关“按照惯例,第一个参数应该指向与正在执行的文件关联的文件名”的部分。这是确保 echo 获得“echo”作为 argv[0] 的部分,这大概取决于它。

After you call exec() or one if its relatives, your original program doesn't exist anymore. That means nothing in that program can affect anything after the exec() call, since it never runs. Maybe you're not building your arguments array correctly? Here's a quick working example of execvp():

#include <unistd.h>

int main(void)
{
  char *execArgs[] = { "echo", "Hello, World!", NULL };
  execvp("echo", execArgs);

  return 0;
}

From the execvp() man page:

The execv(), execvp(), and execvpe() 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.

A common mistake is to skip the part about "The first argument, by convention, should point to the filename associated with the file being executed." That's the part that makes sure echo gets "echo" as argv[0], which presumably it depends on.

太阳哥哥 2025-01-02 10:59:47

请记住,在 exec 调用之后,您的程序将被新程序交换。它不再执行,因此在 exec 调用之后同一进程中的任何代码实际上都是无法访问的。

您确定 b 数组以 NULL 终止吗?最后一个元素必须为 NULL,exec 才能正常工作。另外,请记住将第一个参数也设置为“echo”(它是 argv[0])。

顺便说

execlp("echo", "echo", "something", NULL);

一句,execlp 使用起来更舒服一些,你可以传递任意数量的参数。

Remember, that after exec call your program is exchanged by a new one. It's not executing anymore, so any code in the same process after exec call is, in fact, unreachable.

Are you sure that b array is terminated with NULL? The last element must be NULL for exec to work correctly. Also, remember to set your first parameter to "echo" as well (it's argv[0]).

Try

execlp("echo", "echo", "something", NULL);

Btw, execlp is a bit more comfortable to use, you can pass as many parameters as you wish.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文