Linux 使用 exec() 系列运行“cd”命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9859903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:23:11  来源:igfitidea点击:

Using the exec() family to run the "cd" command

clinuxshellexec

提问by goofy

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

我知道这cd是一个内置的 shell,我可以使用system().

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

但是,这是否可以cdexec()家人运行命令,例如execvp()

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

编辑:我刚刚注意到这system("cd")也是毫无意义的。谢谢大家的帮助。

采纳答案by Kerrek SB

execloads an executable fileand replaces the current program image with it. As you rightly noted, cdis notan 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:

exec加载一个可执行文件并用它替换当前程序映像。正如您正确指出的那样,cd不是一个可执行文件,而是一个内置的 shell。因此,您要运行的可执行文件是 shell 本身。这当然system()对你有用,但如果你想明确说明它,你可以使用exec

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

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

由于这会替换您当前的进程映像,因此您应该在fork()关闭新进程后执行此操作。

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

但是,这整个过程完全没有效果。如果要更改当前进程中的目录,请使用chdir().

回答by orlp

You're better off using int chdir(const char *path);found in unistd.h.

你最好使用int chdir(const char *path);found in unistd.h

回答by Mat

No it is not, and it would be of no use. chdir(the function that changes a process's current directory) only affects the process that calls it (and its children). It does not affect its parent in particular.

不,不是,而且也没有用。chdir(更改进程当前目录的函数)仅影响调用它的进程(及其子进程)。它不会特别影响其父级。

So execing cdhas no point, since the process would exit immediately after having changed directories.

所以execingcd没有意义,因为该进程会在更改目录后立即退出。

(You could exec something like bash -c cd /tmpif you really want to, but as I said, this is fruitless.)

bash -c cd /tmp如果你真的想要,你可以执行类似的操作,但正如我所说,这是徒劳的。)

回答by jlliagre

While, as already stated system("cd xxx")wouldn't change your application current directory, it is not completely useless.

虽然如前所述system("cd xxx")不会更改您的应用程序当前目录,但它并非完全无用。

You can still use system exit status to know if changing your current directory to the one stated would succeed or not.

您仍然可以使用系统退出状态来了解将当前目录更改为指定的目录是否会成功。

Similarly, if you like complex solutions, you could also do the same with fork/exec, either with exec'ing /bin/sh -c cd xxxor simply /bin/cd xxxwith OSes that provide an independent cdexecutable.

同样,如果你喜欢复杂的解决方案,你也可以用 fork/exec 来做同样的事情,要么用 exec'ing,要么/bin/sh -c cd xxx简单地/bin/cd xxx用提供独立cd可执行文件的操作系统。

I would however recommend this non overkill faster equivalent access("xxx", X_OK|R_OK)

然而,我会推荐这种非矫枉过正的更快等价物 access("xxx", X_OK|R_OK)

Note: All POSIX compliant OSes mustprovide an independent cd executable. This is at least the case with Solaris, AIX, HP-UX and Mac OS/X.

注意:所有符合 POSIX 的操作系统都必须提供独立的 cd 可执行文件。至少在SolarisAIX、 HP-UX 和Mac OS/X 中是这样

回答by bnsk

When a fork is done the environment variable CWD(current working directory) is inherited by the child from the parent.If fork and exec is done as usual then the child calls chdir() which simply changes the directory to the new directory and exits but this does not affect the parent.Hence, the new environment is lost..

当 fork 完成时,环境变量 CWD(当前工作目录)由子进程从父进程继承。如果 fork 和 exec 像往常一样完成,那么子进程调用 chdir() ,它只是将目录更改为新目录并退出,但是这不会影响父级。因此,新环境丢失了..