What happens when you type `ls *.c`? into your shell
google

What happens when you type `ls *.c` into your shell

(Note: This post assumes you are aware of the general concepts of?files,?directories,?filename extensions, terminal usage, etc)

What is the shell?: a shell is a computer program which exposes an operating system's services to a human user or other programs.?

First of all, let's see what happens when typing `ls *.c` superficially in the shell:

This is what happens when introducing the ls command:

No hay texto alternativo para esta imagen

As you can see, what the ls command does is list all files and directories in the current directory. However, there are some options added to the command: ′*.c`, the purpose of the wildcard is to find every file/directory that ends or starts with a given string.

Here is an example so you can visualize it:

No hay texto alternativo para esta imagen

The files that ended with ".c" were listed without taking into account the previous part of the file name.

But why don't we go deeper? What really happens when we type ‘ls *.c’ and press enter?

System call definition: a system call is a programmatic way in which a computer program requests a service from the kernel of the operating system on which it is executed.

0) Comand is received:

The shell receives the input through the getline() system call, also a buffer is created to keep the input.

1) Tokenization:

After having the input, the command is tokenized using strtok( ) which allows the shell to analyze each part: "ls" and "*.c".

No hay texto alternativo para esta imagen

2) Checks for aliases:

First of all, the command is searched in the group of aliases and if there is a match, the alias is replaced and executed as the command itself.

3) Checks for built-ins:

When the command has been tokenized, it checks if the command is a built-in or no, and execute it if it actually is.

  • What is a built-in?: in computing, a shell builtin is a command or a function, called from a shell, that?is executed directly in the shell itself, instead of an external executable program
  • What is the PATH?: is a string of characters used to uniquely identify a location in a directory structure.

4) With or without PATH:

Fork() is called to create another process and wait for the other to end. There are two cases left, like for example when the input has the full path or just the command is passed :

First case: /bin/ls
Second case: ls        
No hay texto alternativo para esta imagen

5) Both cases:

In the first case, where the path is available, the execve( ) function is called to basically execute it. However, in the second case, what the shell does is to look for the full path and after having it, call the execve( ) function. Having the path is crucial to know where the executable is.

No hay texto alternativo para esta imagen

6) Final:

If no matches were found, just output an error. In the example below, hola was not found so an error poped up.

No hay texto alternativo para esta imagen

We hope this article has teached you something, thanks for reading!.

Authors: Raymundo Barrera Flores, Gabriela Dominguez and Juan Giménez.

要查看或添加评论,请登录

Juan Giménez的更多文章