LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Writing a Unix Shell - Part II - Why ? (https://www.linuxquestions.org/questions/linux-newbie-8/writing-a-unix-shell-part-ii-why-4175698505/)

Victor43 07-30-2021 10:04 AM

Writing a Unix Shell - Part II - Why ?
 
Hello all. I was reading the blog on writing a UNIX shell as seen here. The author of the article states the following:

"If you try to execute the cd command, you will get an error that says:
cd: No such file or directory" AND "The current working directory of the parent has not changed, since the command was executed in the child."

Here is the code for the above comments made by the author. So my question is why does the command not work when executed in the child process ?

Code:

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

char **get_input(char *);

int main() {
    char **command;
    char *input;
    pid_t child_pid;
    int stat_loc;

    while (1) {
        input = readline("unixsh> ");
        command = get_input(input);

        if (!command[0]) {      /* Handle empty commands */
            free(input);
            free(command);
            continue;
        }

        child_pid = fork();
        if (child_pid == 0) {
            /* Never returns if the call is successful */
            execvp(command[0], command);
            printf("This won't be printed if execvp is successul\n");
        } else {
            waitpid(child_pid, &stat_loc, WUNTRACED);
        }

        free(input);
        free(command);
    }

    return 0;
}

char **get_input(char *input) {
    char **command = malloc(8 * sizeof(char *));
    char *separator = " ";
    char *parsed;
    int index = 0;

    parsed = strtok(input, separator);
    while (parsed != NULL) {
        command[index] = parsed;
        index++;

        parsed = strtok(NULL, separator);
    }

    command[index] = NULL;
    return command;
}


shruggy 07-30-2021 10:58 AM

The answer is located in the linked article one line below the quoted part:
Quote:

The reason behind this is that it is not a system program like ls or pwd.
Given the question you once asked here on LQ, How to check if the command CD is built into the Shell and not an external binary (command), you should have known this.

Victor43 07-30-2021 12:59 PM

Quote:

Originally Posted by shruggy (Post 6270973)
The answer is located it the linked article one line below the quoted part:

Given the question you once asked here on LQ, How to check if the command CD is built into the Shell and not an external binary (command), you should have known this.

Thanks for the reply. I do understand the difference between a system command and a builtin command but what difference does executing this "cd" command in the child make as opposed to in the parent ? Why cannot the cd command be implemented in either in the parent process or in the child process regardless ?

computersavvy 07-30-2021 01:13 PM

Very simple if you think of where you are...... Because the child shell does not contain that built-in command whereas the parent does contain it.

IIF the child is a clone of the parent then it contains the same commands. If it is not a clone then it only contains the commands the programmer chose to include.

Wrap your head around this.
Program A is the parent shell. Program B is the child shell (probably written my a different programmer). What is included in A may not be (and likely is not -- at least in the same form) included in B. Really simple to see from the outside.

Program A launches program B then is gone and only what was included in B is now available.

The same applies to any separate program launched by the shell. Only what is intrinsic to the currently running program is available without external system calls.

Victor43 07-30-2021 01:15 PM

Quote:

Originally Posted by computersavvy (Post 6271012)
Very simple if you think of where you are...... Because the child shell does not contain that built-in command whereas the parent does contain it.

IIF the child is a clone of the parent then it contains the same commands. If it is not a clone then it only contains the commands the programmer chose to include.

Thanks very much that helps a lot.


All times are GMT -5. The time now is 12:07 PM.