Sunday, November 24, 2019

What happens when ls-l is executed?

What happens when ls-l is executed in your command line?



Before we dive into what this Unix command do, lets first find out what the shell is. The shell is a command line interpreter or also known as "shell",  that provides a command line user interface for Unix operating systems. The shell is an interactive command language and a scripting language, used by operating systems to control execution of shell scripts. Think of the shell as you would normally interact with a Windows or MAC, the only difference being that instead of clicking and scrolling to navigate around, you use commands to do the same thing. The simplest way to describe shell is, a program that employs commands.
Now you are probably wondering, what is a shell script? well, a shell script is a program designed to be run using a Unix like operation system. The uses for a shell script are file manipulation, program execution, and printing text. The term is also used more generally to mean the automated mode of running an operating system shell.
This is what it looks like 


There are different types of shell. For example: 

  • The KornShell (ksh).
  • The Bourne Shell (sh, and oldest shell in use).
  • C Shell (csh).
  • Bash (bash).
  • Remote Shell (rsh).
  • Secure Shell (ssh).
  • Almquist Shell (ash).
  • Power Shell (msh).
  • Z Shell (zsh).
If you want to know more information about these types of shell, use google!


Differences between a Graphic user interface and a command user interface.


As we explained earlier, the main difference between a Graphic User Interface and a Command User Interface is the fact that with one you use the mouse and keyboard to navigate in your computer and the other one uses a command prompt where you will input commands to navigate. 

They look like this: 
 
On the left is a visual way of interacting with a computer using items such as windows, icons, and menus, used by most modern operating systems, and on the right we use commands to interact and navigate withing the system in your computer. Visually interacting with windows involve clicking on a folder to open it, while on the command line we use the command ls to display all files in the working directory. 

Inside the Shell

Now that we know how the shell works, lets take a look on how it makes commands and processes happen. As the user, every time you type a command , you hit the enter key. This will tell your computer that an action is being processed and then the computer returns in the output the result of the command you entered. For example: 

When you type the ls command in your terminal, the system will look for it in all its archives until it finds it and then returns an output that displays all files in your working directory. Like this:


But that is not all there is to it. When we type ls with the option -l the system recognize it like this 
ls -l\n. The \n represent a new line in the command line. The system then will replace the \n with a \0 which represents a NULL terminator for the command. This lets the system know where the command ends and how many of them there are. Then the system uses the function strtok() to parse  the command line and  to an array of strings and separating ls and -l into individual strings. Last both of the strings will be concatenated for execution. 

THE PATH 

But how does it find where this command is located or even what it is? The answer lies in PATH.
PATH is an environmental variable that tells the shell in which directories to look for executable files in response to a command input by the user.  Most commands users will enter when operating a shell exist within PATH. First the computer will look if the entered command is a built-in or an alias, if the command is not either of those then it will look into PATH for an executable. 

PATH looks like this: 

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

The directories above contain most of the command you would normally use when interacting with the shell. You may have noticed that all the directories are separated by a ":", more on that in a bit. When we execute the command ls we are looking into the /bin/ls and that will be its full PATH.
Now that we know where to find ls, how do get there? The system takes these steps.

Step 1: The system loops through all the Environmental variables until we find PATH.
Image result for environmental variables in shell

Step 2: The system uses strtok() to look for ":" and then separate each directory into an array of strings.

Step 3: The system concatenates ls to each of the directories until we hit /bin/ls.

That's it!

Now that we have the full PATH we can attach the -l option to it and execute them all together and then the system returns this output:

Once the process is finished we return to the command prompt that waits for the next command form the user input.


By: Andres Cheung & Nicholas Uchida.



 

1 comment:

  1. Devops Training in Hyderabad. DevOps is a set of practices that combines software development and IT operations. It aims to shorten the systems development life cycle and provide continuous delivery with high software quality.
    DevOps Training in Hyderabad

    DevOps Course in Hyderabad

    ReplyDelete