我正在做一个项目,遇到了一个小问题。它被设计成一个简单的 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/