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

标签 c++ shell unix terminal chdir

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

相关文章:

c++ - 使用非类型可变参数模板的运行时计算

c++ - Struct in Struct with vectors 段错误

python - 如何在 Linux shell 脚本或 python 中找出上周六的日期?

linux - FTP 特定文件

c++ - Windows 10 标题栏不工作

c++ - 如何在 C++ 中将精度设置为 100 位?

shell - 用 sed 替换单个空格而不接触多个空格

shell - shell ksh函数参数包含另一个要调用的函数名称

linux - 将 1.txt、2.txt ... 10.txt 连接成一个文件

linux - 避免杀死启动脚本的进程的 Shell 脚本