gpt4 book ai didi

c++ - Unix 自定义 shell 中的 chdir

转载 作者:行者123 更新时间:2023-11-28 00:06:29 25 4
gpt4 key购买 nike

我正在做一个项目,遇到了一个小问题。它被设计成一个简单的 shell,现在,我正在努力输入内置命令(pwd、cd、exit)。除了 CD 之外,我已经得到了所有的工作。

解析有效,命令和参数的不同部分被放入一个数组中,所以现在我有这个:

void cd()
{
chdir(commands[1].c_str());

reset();
}

我运行它,使用该目录中的有效路径,但它没有改变。我错过了什么?

谢谢!

最佳答案

comment :

Can tell directory doesn't change because immediately after, when I run a PWD, (getenv("PWD")), it still shows the original directory that the program was launched in.

您对当前工作目录的检查不正确。参见 chdir() not affecting environment variable PWD

不要使用 getenv("PWD"),而是使用 getcwd

关于c++ - Unix 自定义 shell 中的 chdir,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35424178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com