Unix 中自定义 shell 中的 chdir

chdir in a custom shell in Unix

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

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

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

  reset();
}

我 运行 那个目录中有一个有效路径,但它不会改变。我错过了什么?

谢谢!

:

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.