Home > Linux Commands > The shell and command line

The shell and command line

The shell is a command interpreter that sits between computer and user. The user gives a command (or series of commands grouped together in a script similar to the files.’s Bat dos) and the shell interprets it and sends it to the operating system.

It often tends to underestimate the shell prefers the use of graphical tools, but it may happen to be in critical situations where the graphical tool is unavailable or insufficient. Know the major commands, able to move without the help of a mouse or something graphic, becomes indispensable. In this guide we’ll cover the basic commands for knowing how to move the directories (folders), knowing manipulate files we deal with the redirection and pipes, variables, will manage the jobs.

Under Linux there are various types of shell but the most widely used and discussed in this guide is BASH (Bourne Again Shell).

THE LINE OF CONTROL AND SPECIAL CHARACTERS

The command line is usually composed of two parts: the first contains information about the logged on user, while the second is composed of a string that defines the commands given to the shell. The same is as follows:

[zendune@localhost zendune]$ 

The presence of the character ”$” is not accidental: it tells the user that the shell will send the commands given in administrative mode. When root (system administrator) will launch a shell, the same will present in place of a character”$” ”#”. After the above character, the cursor is waiting for commands that the user wants to perform.

The basic command that you learn in every operating system is the visualization of the directory (folder for those coming from Windows) to start. The command l (lowercase letter L-shaped) has appealed to us showing the output as in Fig.

As you can see, for Mandrake, the output is colored by the type of the file: dark blue for directories, blue for links, green for executable files (with the attribute + x), red ones stored ( . zip,. tar, etc …) and white for everyone else. To test, try giving the command-in that displays all files, even those hidden by an identified”.” preceding the file name.

To be able to move within the structure of the disc should I use the cd command followed by the name of the directory, as shown in the figure. To return to the previous directory (meaning daughter of the current directory) just give the command cd .. (Pay attention to the space between cd ”and” ”..”).

Remove a file is equally simple with the command rm filename. The same command can be used to remove directories.
Often, however, do not remember the full name of the file is recalled, or only its extension. The shell allows the use of special characters that”*” (asterisk),”?” (question mark) and”['''']” (left bracket and right bracket). The asterisk indicates all characters, the question mark a single character in square brackets and a set of specified characters within the brackets. Here are some examples below.

As has been noted, the use of special characters is very useful not only at the display, but also copy, delete or remove (see the mv command.

Standard input, standard output and standard error
UNIX is imposed when it was created the concept of separation between logical and physical organization of the implementation files. In fact, a file written on the hard disk is divided in the presence blocks randomly placed on the mass memory (physical organization), while the user sees the file as a continuous sequence of bytes (logical organization). This way of seeing the file is the basis for everything exists in Linux: all devices are files, a sequence of bytes of input data (input) and output data (output). It ‘can then move the data so that they enter into a device and leave it under a different device. An example is the keyboard and monitor. The keys are pigiamo keyboard input and the output is sent as output to the input of the screen that shows them.

In Linux, there are three channels: standard input (which is normally the keyboard), standard output (which is normally the screen) and standard error (a variant of the standard output).

THE REDIRECTION AND PIPE

The data flow that moves on the computer is due to a file so that a continuous sequence of bytes. This flow can be redirected by means of specific operators: “<" (less than symbol) is the standard input redirection operator, ">” (greater) is the standard output redirection operator, “2>” or ” > & ‘redirection operators are the standard error.

The standard input is used primarily to give a command as input a file (in all other cases we use the keyboard). Suppose Vole see the contents of the file test.txt using the cat command. Here is an example in the figure.

Most commonly used is the standard output when you want, for example, redirect the output from the screen to a file. Note that other figure. The outcome of the first block of the transaction ps aux | grep kmix extrapolates from the list of those processes with the string “kmix” and the output is redirected to the file test.txt. In fact the file is created (overwriting the existing one) with the results of the operations described above. E ‘can still append the result to the file test.txt without overwriting its contents via the operator “>>”. From the command prompt type the ps aux | grep kmix>> test.txt.

And if during the execution of the command errors occur? The cat command displays the contents of files on the monitor, let’s say you view the file does not exist prova_non_esiste.txt and redirect the output to the file prova2.txt. The command cat will prova_non_esiste.txt> prova2.txt.L ‘outcome is obvious: the file will be empty and prova2.txt will appear on the screen that the file does not exist prova_non_esiste.txt. If you wanted to create a series of commands or to debug a set of instructions, you can redirect the errors to a file rather than on the screen. Modify the example above and type cat prova_non_esiste.txt> prova2.txt 2> error. prova2.txt will be empty and will contain the error message warning that the file does not exists.

In addition to the redirection commands can be chained together, generating a stream of data that is output from the first command and the input of the second. To create this flow is used to the pipe. In the example above we just introduced the pipe indicated by “|” character. The ps command, used together with the parameters “aux”, lists the active processes, while the grep command searches for the string “kmix”. By combining the two commands using pipes 2 have requested to the shell to show all processes which have registered as a substring “kmix”.

Another simple example is to show the list of files in a directory, taking care to display as many rows as granted by the active console. The command will be l-al | more.

VARIABLES

During the execution of a command or, more generally, of a program, you may need to assign values ​​to temporary variables, and these were the existence of life within the shell.

There are already set variables such as the search path when you execute a command. To view this variable simply type the command echo $ PATH. The echo command displays the value of the PATH variable (preceding the variable name from the character “$”).

To create a variable just give it a label and assign the value using the following syntax variable_name = valorefacendo careful not to put spaces before or after the “=”. In case the value may contain blank spaces will be used to double quotes, the following sentence = “My name zendune”.
To see all the variables set in the shell just type the set command. To remove a variable you use the unset command as follows: unset test.

The JOBS

When we type a command (let’s call to distinguish it from the job processes) and press the “send”, but these runs, as we piloted the input and output, we can also monitor the execution. Some commands are complex and if undertaken, would prevent the use of the shell until they are completed. It ‘can then start the command in the background and have the shell again free for other uses, you can call the command in the foreground, or suspend or cancel it.

To run the command in the background, simply insert the end of the same character “&”. If you wanted to print the file test.txt in the background would be enough to give the command lpr test.txt &. The operating system assigns a unique number to the job and start marking it with a “+” (active job). In the case under a new command, the latter would be assigned the next number and marked with the sign “-” (awaiting execution).

To see how many jobs are running, just getting the jobs command to give output in Fig.

It ‘can then be able to return to the command in the foreground using the fg command followed by the character “%” and the number of jobs. For example, if you want to bring to the foreground the second command in the queue, you would type fg% 2. The command can not return to its conclusion in the background. E ‘can still get around this limitation by suspending the job with the key combination “Ctrl Z” and restarting it by fg or bg.

A command ended not warn the user of the conclusion of their work if not explicitly indicated by notify% 1 (in this case advises the termination of the first command).

It ‘can then proceed to the forced termination of the command if, for example, to enter into an infinite loop. Linux provides the kill command followed by the number that identifies the job (eg% 1).

CONCLUSION

This brief guide is intended to provide a basic understanding of shell and the ability to move among the various folders in which you organized our storage. It may seem trivial or unnecessary, but as indicated in some cases you need to make: a block to the X server and you find yourself in text mode (in a shell) or, after installing the Java VM, it does not work because the variable $ PATH environment is not properly set.

Who would like more of BASH commands can refer to this link http://ildp.pluto.it/guide/abs/index.html, depth guide course in Italian.

  1. No comments yet.
  1. No trackbacks yet.