我知道 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"命令
发布评论