使用exec()系列运行" CD"命令

编程入门 行业动态 更新时间:2024-10-28 18:25:10
本文介绍了使用exec()系列运行" CD"命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道 CD 是内置了壳,我可以用系统()运行

I know that cd is a shell built-in ,and I can run it by using system().

但是,这是可能通过来运行 CD 命令执行exec()家庭,如 execvp()?

But is that possible to run the cd command by the exec() family, like execvp()?

编辑:我只注意到系统(CD)也是大家的帮助下meaningless.Thanks

And I just noticed that system("cd") is also meaningless。Thanks for the help of everyone.

推荐答案

EXEC 加载一个的可执行文件的并用它替换当前程序映像。当你正确地指出, CD 是的不的可执行文件,而是一个shell内建命令。所以,你要运行的可执行文件是shell本身。当然,这是什么系统()为你做,但如果你想明确一下,你可以使用 EXEC :

exec loads an executable file and replaces the current program image with it. As you rightly noted, cd is not an executable file, but rather a shell builtin. So the executable that you want to run is the shell itself. This is of course what system() does for you, but if you want to be explicit about it, you can use exec:

execl("/bin/sh", "-c", "cd", (const char *)0);

由于此的替换的您当前的进程映像,你应该在做到这一点叉() ING掀起了新的进程。

Since this replaces your current process image, you should do this after fork()ing off a new process.

不过,这整个过程中完全没有效果。如果要更改目录在当前过程中,使用 CHDIR()。

However, this entire procedure has absolutely no effect. If you want to change the directory in your current process, use chdir().

更多推荐

使用exec()系列运行" CD"命令

本文发布于:2023-07-16 03:58:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117617.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   系列   exec   CD   QUOT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!