C 中 execlp() 的基本用法

发布于 2024-12-22 01:56:07 字数 345 浏览 1 评论 0原文

所以我在课堂上得到了这个代码:

execlp("sh", "sh", "-c", string, (char*) 0);

并被告知要理解它。我一直在疯狂地查找 execlp API,但我仍然不完全理解这段代码在做什么。

我最好的猜测: “sh”是文件路径

“-c”是参数

字符串是命令

(char*)0 是空终止符。

我似乎无法弄清楚为什么代码中有两个“sh”实例: 我猜测 -c 意味着复制,并且该命令正在使用字符串命令执行从一个文件到另一个文件的复制,但我对此并不是 100%。

任何澄清将不胜感激。 谢谢。

So I was given this code:

execlp("sh", "sh", "-c", string, (char*) 0);

In class, and told to udnerstand it. I've been looking up the execlp API like crazy, but I still don't fully understand what this code is doing.

My best guess:
"sh" is the filepath

"-c" is the argument

string is the command

(char*)0 is the null terminator.

I can't seem to figure out why there are two instances of "sh" in the code:
I was guessing that -c means copy, and that the command is executing a copy from one file to another, using the string command, but I'm not 100% on that.

Any clarification would be greatly appreciated.
Thank you.

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

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

发布评论

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

评论(1

隔纱相望 2024-12-29 01:56:07

第一个参数是要执行的文件名(在本例中是 sh 命令)。它的第一个参数应该是可执行文件的名称(因此重复“sh”),-c 是 sh exe 的另一个参数,告诉它从命令字符串读取命令。 string 是(我猜)sh 应该执行的命令,而 (char *)0 只是一个结束标记。

如果我遗漏了您想要解释的内容,请告诉我。

The first argument is the file name to execute (in this case it's the sh command). The first argument to it should be the name of the executable (hence the repeated "sh"), -c is another argument to the sh exe that tells it to read commands from the command string. string is (I'm guessing) the commands that sh should execute and the (char *)0 is just an ending marker.

Let me know if I left something out you wanted explained.

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