如何在自己的shell中实现自己的cd命令 [英] How to implement your own cd command in your own shell
问题描述
我正在做一个小型项目:我自己的命令解释器(外壳)",例如Bash外壳程序或Sh外壳程序.到现在为止,它可以执行每个预定义的命令,例如ls,ps,pwd,date.除此之外,我还实现了其他一些操作,例如Input Redirection(<),Output Redirection(>),PIPE(|)功能.与此同时,我实现了自己的用户命令,例如pid,ppid,quit,hist.
I am working in a mini project: "MY OWN COMMAND INTERPRETER (SHELL)", like the Bash shell or Sh shell. Till now it can execute every predefined commands like ls, ps, pwd, date. Except this I have implemented some other operations like Input Redirection(<), output redirection(>), PIPE (|) features. Along with this i have implemented my own user-commands like 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命令时遇到了一些困难...
But I am facing some difficulty in executing the cd command...
<12 SOURABH> cd
cd: No such file or directory
<13 SOURABH> cd dir
cd: No such file or directory
<14 SOURABH>
有人告诉我自己实现它...请帮助我这样做.预先感谢.
Someone told me to implement it by my own... Please help me in doing this. Thanks in advance.
推荐答案
cd
不是程序,它是内置命令.内置命令直接内置到外壳中.
cd
is not a program, it's a built-in command. The built-in command are directly built into the shell.
要实现cd
,您基本上需要
- 使用
chdir()
更改当前目录 - 更新一些环境变量(例如Klas Lindback所说的:
OLDPWD
和PWD
)
- Use
chdir()
to change the current directory - Update some environment variables (like Klas Lindback said:
OLDPWD
andPWD
)
这篇关于如何在自己的shell中实现自己的cd命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!