LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-30-2021, 10:04 AM   #1
Victor43
Member
 
Registered: May 2020
Posts: 46

Rep: Reputation: Disabled
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;
}
 
Old 07-30-2021, 10:58 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
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.

Last edited by shruggy; 07-30-2021 at 02:57 PM.
 
Old 07-30-2021, 12:59 PM   #3
Victor43
Member
 
Registered: May 2020
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
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 ?
 
Old 07-30-2021, 01:13 PM   #4
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
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.

Last edited by computersavvy; 07-30-2021 at 01:30 PM.
 
Old 07-30-2021, 01:15 PM   #5
Victor43
Member
 
Registered: May 2020
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by computersavvy View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Writing Better Shell Scripts - Part 3 LXer Syndicated Linux News 0 09-25-2010 03:30 AM
LXer: Writing Better Shell Scripts – Part 2 LXer Syndicated Linux News 0 07-26-2010 07:50 PM
LXer: Writing Better Shell Scripts – Part 1 LXer Syndicated Linux News 0 06-15-2010 02:20 PM
LXer: Speaking UNIX Part 8: UNIX Processing LXer Syndicated Linux News 0 06-14-2008 04:51 AM
LXer: Speaking UNIX, Part 4: Setting and managing permissions on UNIX LXer Syndicated Linux News 0 10-21-2006 01:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration