xv6 shell

1、在home下新建shell.c文件,拷贝原有代码,并修改runcmd部分:

void runcmd(struct cmd *cmd)
{
    int p[2], r;
    struct execcmd *ecmd;
    struct pipecmd *pcmd;
    struct redircmd *rcmd;
    if(cmd == 0)
        exit(0);
    switch(cmd->type)
    {
    default:
        fprintf(stderr, "unknown runcmd\n");
        exit(-1);
    case ' ':
        ecmd = (struct execcmd*)cmd;
        if(ecmd->argv[0] == 0)
            exit(0);
      // fprintf(stderr, "exec not implemented\n");       
      // system(ecmd->argv[0]);  // Your code here ...
      // execl("/bin/sh", "sh", "-c", ecmd->argv[0], (char *)0);
     execvp(ecmd->argv[0],ecmd->argv);
     break;
    case '>':
    case '<':
        rcmd = (struct redircmd*)cmd;
        FILE *stream;
        if(rcmd->fd == 1)
        stream  =  freopen(rcmd->file,"w",stdout);     
        else
           freopen(rcmd->file,"r",stdin);      
          if(stream == NULL)
             fprintf(stderr,"errorredirecting\n");
         else{
               runcmd(rcmd->cmd);
           fclose(stream);
         }
        break;
    case '|':
        pcmd = (struct pipecmd*)cmd;
        int p[2];
        if(pipe(p) < 0)
           fprintf(stderr,"pipe not implemented\n");   
        if(pipe(p) == 0){
           if(fork1() == 0){
              close(1);
              dup2(p[1],1);
              close(p[0]);
              runcmd(pcmd->left);
         }
        else{
          close(0);
          dup2(p[0],0);
          close(p[1]);
           runcmd(pcmd->right);
        }
   }
     // Your code here ...
        break;
    }
    exit(0);
}

2、在home下新建t.sh文件,输入如下代码:

ls > y
cat < y | sort | uniq | wc > y1
cat y1
ls | sort | uniq | wc
rm y

3、运行结果如下图所示:

这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值