使用exec()系列运行" CD"命令 [英] Using the exec() family to run the "cd" command

查看:174
本文介绍了使用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"命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆