简体   繁体   English

在C ++中为Shell实现bash运算符

[英]Implementing bash operators for shell in c++

I'm trying to implement the || 我正在尝试实现|| , && , and ; &&; operators in a bash shell I am making. 我正在制作的bash shell中的运算符。 What I am trying to do, is use a int as a flag that would be set to 1 if it was successful or 0 if it was not. 我正在尝试做的是将int用作标志,如果成功将设置为1,否则将设置为0。 My issue is that even if I enter an invalid operation such as ls -apples it sets the flag to 1. I also get the error message 我的问题是,即使我输入了ls -apples之类的无效操作,它也会将该标志设置为1。我也会收到错误消息

ls: invalid option -- 'e' Try 'ls --help' for more information ls:无效选项-'e'尝试使用'ls --help'了解更多信息

so I think that means it technically is executing? 所以我认为这意味着它在技术上正在执行? How do I keep track of whether execvp took a invalid operation? 如何跟踪execvp是否执行了无效操作? Here is my code: 这是我的代码:

    pid_t pid;
    pid_t waitId; 
    int status; 
    //forks into two processes 
    pid = fork();
    //There was an error during fork
    if (pid < 0) 
    {
        successFlag = 0; 
        perror("There was an error");
    } 
    else if (pid == 0) 
    {
        //must be cast because our function expects a char *const argv[]
        if (execvp(command[0], (char**)command) < 0) 
        { 
            //error at execvp
            successFlag = 0; 
            perror("There was an error executing the process");
        }
            exit(EXIT_FAILURE);
    }

    else 
    {
        do 
        {
          waitId = waitpid(pid, &status, WUNTRACED | WCONTINUED);
          if(waitId == -1){
              successFlag = 0; 
              perror("Error in parent process");
              exit(EXIT_FAILURE);
          }

        } 

        while (!WIFEXITED(status) && !WIFSIGNALED(status));
    }
    //use this flag to determine whether the process was a success
    successFlag = 1;

The solution was to look at the number that status returns. 解决方案是查看status返回的数字。 The sign would tell you whether it was successful or not. 该标牌会告诉您是否成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM