dup(fd[0]);
close(fd[0]);
waitpid(pid, &status, 0);
pipel(other); /*执行后面的命令*/
}
注意到,我的管道实际上是先先做完第一条管道前的内容,然后再
去执行后面的 command,而非由“|”分隔的语句同时执行。
最后是重定向的实现。重定向完成的是实现进程的标准输入和输出
的定向问题。我对于重定向的语法理解是:
redirectcommand := command ">" outfile |
command "<" infile |
command "<" infile ">" outfile |
command ">" outfile "<" infile |
command
因此可以通过分析“>”“<”号的出现顺序,决定 infile、outfile
的位置。在具体实现时,用一个 type 变量来表示属于哪一种重定向:
type:3: < infile
4: > outfile
5: > outfile < infile
6: < infile > outfile
type 初始时定义为 2,
for(i=0;i<strlen(cmd);i++){
if(cmd[i]=='<')
type++;
if(cmd[i]=='>')
type=type*2;
}
Optional 的实现:
主 要 通 过 #include
<readline/readline.h>#include<readline/history.h>来实现。
GNU Readline is the library used to make advanced command-line wizardry
convenient and conistent across a multitude of command-line applications.
These programs include bash, bc, ftp, gnuplot, gpg, ksh, mysql, psql, python,
smbclient, xmllint and zsh.
在具体实现时,只要在 main 函数里添加几行代码:
initialize_readline ();
line = readline ("shell$: ");