O Shell, uma concha entre l


#Linux #Shell

  • What is the Linux Shell

  • System indicator or command prompt

  • The command line concept

  • What are the standard inputs / outputs

  • Redirects

  • Communication tubes

What is the Linux Shell

The command interpreter is the interface between the user and the operating system, which is why its English name is shell (shell):

Shell, a shell between l


The Shell interface acts as the intermediary between the operating system and the user thanks to the command lines written by him. Its function is to read the command line, interpret its meaning, execute the command and return the result through the outputs. In fact, the Shell interface is an executable file, responsible for interpreting commands, transmitting them to the system and returning results.

There are several types of Shell, the most common being sh (called Bourne shell), the bash (Bourne again shell), the csh (C Shell), the Tcsh (Tenex C shell), the ksh (Korn shell) and the zsh (Zero shell). Usually, their names correspond to the name of the executable.

Each user has a standard Shell, which will be activated when opening a command indicator. The default Shell is defined in the configuration file

/etc/passwd

in the last field of the line that corresponds to the user. You can change the Shell during a session. To do this, simply run the corresponding executable file, for example:

/bin/bash

.

System indicator or command prompt

The Shell starts by reading its global configuration (in a file in the / etc / directory). Then, it reads the user’s own configuration (in a hidden file, whose name begins with a period and is located in the user’s basic directory:

 /home/user_name/.configuration_file

). Then, the following command indicator (prompt) appears:

máquina:/diretório/atual$

.

By default, in most Shells, the prompt is made up of the machine name, followed by a colon (:), the current directory and a character that indicates the type of user logged in: $ indicates that he is a normal user and # administrator, called root.

The command line concept

A command line is a character string consisting of a command that corresponds to an executable system file, or rather, a Shell command as well as the optional settings:

ls -al /home/jf/

.

In the command above, ls is the name of the command and Al and / home / jf / are the settings. Those that start with are the options. For each command, there are a number of options that can be detailed by entering one of the following commands:

command --help

and

command -?  man command

.

What are the standard inputs / outputs

During the execution of a command, a process is created that will open three streams:

stdin, called standard input, in which the process reads the input data. By default, the stdin refers to the keyboard and is identified by the number 0;

stdout, called standard output, in which the process writes the output data. By default, the stdout refers to the screen and is identified by the number 1;

stderr, called standard error, in which the process writes the error messages. By default, the stderr refers to the screen and is identified by the number 2:

standard input-outputs: STDOUT, STDIN, STDERR


By default, when a program is run, data is read from the keyboard and the program sends its output and errors to the screen. However, it is also possible to read the data from any input device, or even from a file, and send the output to a display device, file etc.

Redirects

Linux, like any Unix system, has mechanisms that allow you to redirect standard input / output to files.

Thus, the use of the character > allows you to forward the standard output of a command located on the left of a file located on the right. See the example:

ls -al /home/jf/ > toto.txt  echo "Toto" >/etc/meuarquivodeconfiguração

The following command is equivalent to a copy of the files:

cat toto > toto2

The purpose of redirection > is to create a new file. So, if a file of the same name exists, it must be crushed. The following command simply creates an empty file:

> arquivo

The use of a double character >> allows you to concatenate the standard output to the file, that is, add the output after the file, without deleting it.

In the same way, the character < indicates a redirection of standard input. The following command sends the contents of the file toto.txt with the command cat, whose sole purpose is to show the content in standard output (the example is not useful, but instructive):

cat < toto.txt

Finally, the use of redirection << allows reading, in standard input, when the string is on the right. In the following example, the standard input is read until the word STOP is found. Then the result appears:

cat << STOP

Communication tubes

The tubes (pipes) are communication mechanisms specific to all UNIX systems. A tube, symbolized by a vertical bar (character |), allows to assign the standard output of one command to a standard input of another, in the same way that a tube allows communication between the standard input of one command and the standard output of another .

In the following example, the standard output of the command ls – Al is sent to the program sort in charge of sorting the results in alphabetical order:

ls -al | sort

This allows to connect a certain number of commands by successive tubes. In the following example, the command will show all files in the current directory, select the lines that contain the zip extension (thanks to the grep command) and count the total number of lines:

ls -l | grep zip | wc -l

Related Posts