类项目ubuntu上的Unix shell。无法识别错误

类项目ubuntu上的Unix shell。无法识别错误,shell,unix,fork,echo,Shell,Unix,Fork,Echo,我的密码在这里。基本上,定制shell需要将echo、cd和quit作为命令,并为这些命令派生一个子命令。编译时没有错误,但未运行。当我给出,说“echo hello”时,它不响应参数…它进入了正确的函数,但我无法指出错误。我猜我在使用execlp函数时出错了。有人能帮我吗 #include <stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #inc

我的密码在这里。基本上,定制shell需要将echo、cd和quit作为命令,并为这些命令派生一个子命令。编译时没有错误,但未运行。当我给出,说“echo hello”时,它不响应参数…它进入了正确的函数,但我无法指出错误。我猜我在使用execlp函数时出错了。有人能帮我吗

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>

int main()
{

    char command[256];
    char * parsedCmd;
    char * argument;

    //char *sep[]=" ";

    while (1<2)
    {
        printf("\nOS Assignment 1@user: ");
        fgets(command, sizeof(command), stdin); 

        parsedCmd=strtok(command, " ");
        argument=strtok(NULL, " ");

        //printf("\n%s\n", argument);

        if (strncmp("quit", command, 4)==0)
            break;
        if (strncmp("cd", parsedCmd, 2)==0)
        {
            printf("\nExecuting cd\n");
            execCD(argument);   
        }
        if (strncmp("echo", parsedCmd, 4)==0)
        {
            printf("\nEchoing now...\n");
            shellEcho(argument);
        }       
        else
            printf("\nOur shell is simple. Try either cd, echo or quit :) ...\n");  
    }   
}

int execCD(char *receive)
{
    printf("\nExecuting cd as Child...\n"); 
    printf("\nDirectory to cd is %s\n", receive);
    pid_t pid;

    pid=fork();

    if (pid<0)
    {
        fprintf(stderr, "\nFork Failed\n");
        return 1;
    }

    else if (pid==0)
    {
        execlp(receive, "cd", NULL);
    }

    else
    {
        wait(NULL);
        printf("Child Complete");
    }

    return 0;
}

int shellEcho(char *receive)
{
    printf("\nExecuting echo as Child...\n");   

    pid_t pid;

    pid=fork();

    if (pid<0)
    {
        fprintf(stderr, "\nFork Failed\n");
        return 1;
    }

    else if (pid==0)
    {
        execlp(receive, "echo", NULL);
    }

    else
    {
        wait(NULL);
        printf("Child Complete");
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
int main()
{
char命令[256];
char*parsedCmd;
字符*参数;
//字符*sep[]=“”;

while(1您有向后执行lp的参数。另外,请注意,按照惯例,您需要指定“echo”两次:一次作为要执行的程序的名称,第二次作为第0个参数(通常是但不一定是程序的名称)

int shellEcho(字符*接收)
{
printf(“\n作为子项执行echo…\n”);
pid_t pid;
pid=fork();

如果(PID)打开?代码确实存在问题,海报试图(也是成功的)确定问题所在。是的,我会与你的老师讨论这个问题。
int shellEcho(char *receive)
{
    printf("\nExecuting echo as Child...\n");   
    pid_t pid;
    pid=fork();

    if (pid<0) {
        fprintf(stderr, "\nFork Failed\n");
        return 1;
    } else if (pid==0) {
        execlp("echo", "echo", receive, NULL);
    } else {
        wait(NULL);
        printf("Child Complete");
    }

    return 0;
}