) will be removed from the end of your command.
It will be replaced with a null byte, , which signifies the end of a command.
This will let the computer know how many commands and flags to expect.
Once this is done a function called strtok() will parse the command into an array of strings.
Essentially it separates ls and -l into their own individual spots.
The original command ls, and the whole concatenated string ls & -l, will need to be saved later on when it’s time to execute.
More on that later.
The Power of the PATHMost commands users will enter when operating a shell exist in the PATH.
However, before we look at the PATH we need to check to see if the entered command is an alias or a built-in command.
If the entered command is either of these things then the computer will execute them using a different process.
If the entered command is neither an alias or a built-in, then it will be compared to the PATH.
The PATH is a string of directories separated by colons.
It looks like this:PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/gamesThese directories hold most of the commands that a user can enter.
So for example, if we’re entering ls, we need to scroll through the PATH and find where ls lives.
In this case, the full path for ls is /bin/ls.
But how can we search through these directories?First, we’ll need to find the correct variable, PATH.
PATH is part of a shell’s environment and is one of many variables.
We’ll need to cycle through all of an environment’s variables until we find PATH.
Once it’s found we then need to separate the directories by parsing them, the same way we parsed ls -l earlier.
For this we’ll use strtok() and search for :s instead of spaces.
We can then save each directory into an array of strings.
Time to CompareOk so now we have an array of strings that holds all of PATHs directories.
Think of it like an easy to read list rather than the long string shown above.
It’ll look something like this:/usr/local/bin/usr/local/sbin/usr/binetc…Professor Cavigli working on his dissertation showing how a command is appended to the end of each directory in a PATH.
ls lives at the end of /bin so we’ll need to scroll through the list of directories, append ls to each directory, and check to see if it exists.
If it exists, then we know we want to execute it.
To do so we scroll through the array of strings, and first concatenate /, then ls.
We’ll check each directory’s full path to see if the file exists.
If it does, then we’ll return the concatenated string.
In this case, /bin/ls.
Execution!At this point we have been running one process to do all this work.
We will now create an identical child process to execute the command.
The original parent process will wait until the child process finishes, whether that be by executing the command or returning an error message.
Now that we have the full path of the executable file, /bin/ls, we’ll send it along with any inputted flags (-l), and try to execute the command.
If it works it will display the contents of a user’s current working directory.
It should look something like this:Once the process has completed we return to the parent process which prompts the user to enter another command if they wish.
With this prompt the user is able to enter more commands and navigate around the shell.
Written by Marc Cavigli and Sofía Cheung.