What happens when you type ls -l in the shell.
Adrian De La Asuncion Buelvas
FrontEnd Web Developer at Mercadolibre | React | Typescript | NodeJs | React Native
In this post I am going to explain what are the commands and the system calls.
What is a Command?
a command is an instruction that we users provide to a computer system, from the command line for example the shell.
There are several types of consoles, some of these are:
command.com for systems based on DOS(like MS-DOS, DR-DOS, IBM PC-DOS, FreeDOS, etc).
cmd.exe for systems based on Windows NT(like 2000, XP, 2003, Vista, Windows 7, etc).
bash, zsh, sh, ksh for systems based on Unix, which are (Mac OS X, GNU/Linux, Android. etc).
I'm going to show you how a shell works below.
the first thing would be to explain what system calls are.
System Calls
GNU / Linux is a multitasking Operating System in which a large number of processes will coexist. It is possible, either due to a programming failure or a malicious attempt, that one of these processes does things that threaten the stability of the entire system. Therefore, in order to protect this stability, the kernel or kernel of the system works in a totally different environment from other programs. Two totally separate execution modes are then defined: kernel mode and user mode.
Each of these modes of execution has different memory and procedures, so a user program may not be able to harm the kernel.
Syscalls or system calls are the mechanism by which user processes and applications access kernel services. They are the interface provided by the kernel to perform things that are typical of kernel mode (such as accessing a disk or using a sound card) from user mode. The following figure graphically explains how the read () syscall works.
The most common system calls are:
- fork( )
- getpid( )
- getppid( )
- the whole family of exec…(…)
- wait( )
- waitpid(…)
- exit(…)
- system(…)
the first thing is to get an instruction or command from the console, to get what we write we are going to use a function that is:
prompt
prompt is a short message, an order that is written in the console, the system takes care of packaging what happened and prints it.
execute the order and show.
getline()
The getline function gets the address of the buffer that would be a double pointer and will store the characters that we pass, it will also receive the address of the size of the bytes that we pass it, and stdin, which is the standard input to be read.
With the buffer received by the getline we can do validations and comparisons so that when we enter a command that does not exist or a wrong character shows me an error.
This is a diagram of how a getline works and its validations:
after doing validations and comparisons we use system calls like fork()
fork()
Is a system call that is used to create new processes in the system.
fork (); // Line 1 fork (); // Line 2 fork (); // Line 3 L1 // There will be 1 child process / \ // created by line 1. L2 L2 // There will be 2 child processes / \ / \ // created by line 2 L3 L3 L3 L3 // There will be 4 child processes // created by line 3
the child processes are created and we execute the orders that we pass through getline.
wait()
wait() is a system call, this call blocks the parent process until the child (child) process exits or receives a signal.
once the child process ends the parent process continues.
We also have to implement PATH so that it can recognize the commands without having to pass the entire PATH.
What is PATH?
PATH is an environmental variable in Linux and other Unix-like operating systems that tells the shell which directories to look for executable files (that is, ready-to-run programs) in response to commands issued by a user. It increases both the convenience and the security of such operating systems and is widely considered to be the most important environmental variable.
For example:
without PATH /bin/ls /bin/ls -l /bin/echo
with PATH /bin/ls /bin/ls -l /bin/echo ls ls -l echo
what are environment variable?
In Linux and Unix based systems environment variables are a set of dynamic named values, stored within the system that are used by applications launched in shells or subshells. In simple words, an environment variable is a variable with a name and an associated value.
Environment Variables and Shell Variables
KEY=value KEY="Some other value" KEY=value1:value2
Shell variables are variables that apply only to the current shell instance. Each shell such as zsh and bash, has its own set of internal shell variables.There are several commands available that allow you to list and set environment variables in Linux:
- env – The command allows you to run another program in a custom environment without modifying the current one. When used without an argument it will print a list of the current environment variables.
- printenv – The command prints all or the specified environment variables.
- set – The command sets or unsets shell variables. When used without an argument it will print a list of all variables including environment and shell variables, and shell functions.
- unset – The command deletes shell and environment variables.
- export – The command sets environment variables.
we execute the instructions with the syscall execve()
execve executes the instructions we pass to it after PATH has validated and recognized them.
After explaining this, I am going to show some of the commands used in the Linux console.
cd changes directory depending on which address you put.
pwd this command shows the route you are currently on
echo this command prints the argument that you put
ls this command serves to list, list the folders or files of the path where you are.
What is a shell alias?
it is a shortcut to reference a command and to avoid writing a long command (an order).
How to use an alias?
An example would be the command ls, ls is a command that lists the files of the current path, to this we will add -l, -l would be the alias which will execute a different process than ls, now that you type the :
ls -l
ls -l command would show me the files and folders that are in the current path and would show me the permissions of each file and folder.
What happens when you type ls -l in the shell
when you type this command it will list the folders and files that are in that path, it will also show you in detail the permissions and.
File Permissions
On a Linux system, each file and directory are assigned access rights for the file owner, members of a related user group, and all others. You can assign rights to read a file, write a file, and run a file (that is, run the file as a program).
Here we can see:
- The file "/bin/bash" is owned by user "root"
- The superuser has the right to read, write, and execute this file
- The file is owned by the group "root"
- Members of the group "root" can also read and execute this file
- Everybody else can read and execute this file
chmod
The chmod command is used to change the permissions of a file or directory. To use it, you specify the desired permission settings and the file or files that you wish to modify. There are two ways to specify the permissions. In this lesson we will focus on one of these, called the octal notation method.
It is easy to think of the permission settings as a series of bits (which is how the computer thinks about them). Here's how it works:
rwx rwx rwx = 111 111 111 rw- rw- rw- = 110 110 110 rwx --- --- = 111 000 000 and so on... rwx = 111 in binary = 7 rw- = 110 in binary = 6 r-x = 101 in binary = 5 r-- = 100 in binary = 4
Authors
Eduardo Vega
Adrian De La Asuncion
References
https://www.linfo.org/path_env_var.html
https://linuxcommand.org/
https://linuxize.com/post/how-to-set-and-list-environment-variables-in-linux/
Full-stack Developer
4 年?Excelente contenido!
Logistic Manager en City Lending Inc
4 年nice job