0

so im trying to create a shell in CentOS (linux C) the new shell needs to be able to use all the existing commands in the original shell and also a few more functions so this is the function to use the usual commands, this works fine just have problam when combining 2 commands together such as if i enter somthing like 'echo hello world > test.txt' whats need to happen is to create a new file called 'test.txt' with the text hello world inside and what really happen is that the shell use the echo command and print to the screen 'hello world > test.txt'

hope that you understand me and you can help thanks.

int regularCmds(char** args, char* inputStr)
{
    int status, i, pid;
    char* split;
    char path[MAX];
    if (strstr(inputStr, "/bin/") != NULL)
    {
        for (i = 0; i < 10; i++)
            args[i] = NULL;
        split = strtok(inputStr, " /");
        i = 0;
        while (split != NULL)
        {
            args[i++] = split;
            split = strtok(NULL, " /");
        }
        strcpy(path, "/bin/");
        strcat(path, args[1]);
        args[0] = args[1];
        args[1] = NULL;
        if ((pid = fork()) == -1) {
            perror("fork");
            exit(1);
        }
        if (pid == 0) {
            execv(path, args);
            printf("\nNot Supported\n");
            exit(EXIT_FAILURE);
        }
        else
        {
            pid = wait(&status);
        }
    }
    else
    {
        strcpy(path, "/bin/");
        if ((pid = fork()) == -1) {
            perror("fork");
            exit(1);
        }
        if (pid == 0) {
            strcat(path, args[0]);
            execv(path, args);
            printf("\nNot Supported\n");
            exit(EXIT_FAILURE);
        }
        else
        {
            pid = wait(&status);
        }
    }
}

  • 1
    Redirection like `>test.txt` is a feature of the shell, not the operating system or the commands like `echo`. If you write your own shell, you have to parse the line of input, and handle those things -- for example creating `test.txt` and then redirect stdout of the `execv` command (eg: see https://stackoverflow.com/questions/36513265/how-to-redirect-content-of-file-to-stdin-why-the-exec-function-doesnt-work) – Paul Hankin Mar 31 '21 at 18:05
  • Is there a question here? You're not doing anything with `>`; just passing it as an argument, So it gets treated as an argument. What would you expect to happen? – Chris Dodd Mar 31 '21 at 18:06
  • I don't think you're going to get a complete answer to your question (unless I've misunderstood it). It's going to require a fair bit of code. – Paul Hankin Mar 31 '21 at 18:08
  • my question is how can i make the shell hanlde the all the usual commands i need to handle each one of the commands seperatly or there is anything else that i can do somthinh like sending the command to the original shell? – Saar Keshet Mar 31 '21 at 18:15
  • see a tutorial explaining how to build linux shell in c : https://medium.com/swlh/lets-build-a-linux-shell-part-ii-340ecf471028. – matan h Dec 20 '22 at 14:19

0 Answers0