如何在自己的 shell 中实现自己的 cd 命令[重复]

2023-12-25

我正在一个小型项目中工作:“我自己的命令解释器(SHELL)”,例如 Bash shell 或 Sh shell。到目前为止,它可以执行所有预定义的命令,如 ls、ps、pwd、date。除此之外,我还实现了一些其他操作,例如输入重定向()、PIPE(|)功能。除此之外,我还实现了自己的用户命令,如 pid、ppid、quit、hist。

vvdnlt208@vvdnlt208-Vostro-3446:~/project/Vector/sourabh/unix/proj2$ ./my_sh  
<1 SOURABH> ls
 a     cd.c       execute.o  inredir.c  main.o   multiexec.o  my_sh   sh.c                sigign.c  test.c
addhist.c  dir        header.h   inredir.o  Makefile     mypipe.c     outredir.c  showhist.c  sigign.o  tetest.c
addhist.o  execute.c  inredir    main.c     multiexec.c  mypipe.o     outredir.o    showhist.o  test2.c
<2 SOURABH> ps
PID TTY          TIME CMD
6120 pts/10   00:00:01 bash
6510 pts/10   00:00:00 grep
6512 pts/10   00:00:00 grep
6514 pts/10   00:00:00 grep
6516 pts/10   00:00:00 grep
7772 pts/10   00:00:00 my_sh
7774 pts/10   00:00:00 ps
<3 SOURABH> pwd
/home/vvdnlt208/project/Vector/sourabh/unix/proj2
<4 SOURABH> date
Fri Nov 14 16:44:24 IST 2014
<5 SOURABH> 
<6 SOURABH> 
<7 SOURABH> 
<8 SOURABH> hist
1 ls
2 ps
3 pwd
4 date
8 hist
<9 SOURABH> ls >a.txt
<10 SOURABH> pid
7772
<11 SOURABH> ppid

但我在执行 cd 命令时遇到一些困难......

<12 SOURABH> cd
cd: No such file or directory
<13 SOURABH> cd dir
cd: No such file or directory
<14 SOURABH> 

有人告诉我自己实现它......请帮助我这样做。提前致谢。


cd不是程序,而是内置命令。内置命令直接内置于 shell 中。

实施cd,你基本上需要

  1. Use chdir()更改当前目录
  2. 更新一些环境变量(就像 Klas Lindback 所说:OLDPWD and PWD)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在自己的 shell 中实现自己的 cd 命令[重复] 的相关文章

随机推荐