The Linux shell

The Linux shell

A shell or command interpreter is a program interface that allows a user to write instructions to the operating system. In the Linux environment there are three major families of shells: sh, csh and ksh. The bash shell (Bourne Again Shell) belongs to the family sh

One of the main features of a command interpreter is that it can be programmed using text files that contain commands that are to be interpreted in the execution of the same

These text files are called files commands or scripts (also scripts, shell scripts or scripts shell). Therefore, a file of commands is a text file executable that contains commands that can be interpreted by the shell

Features

The first line of the file indicates the type of shell that must be interpreted, in our case bash:

Each line of the file contains a command that will be executed by the interpreter

If you want to run multiple commands on the same line must be separated by ‘;’

To continue a command in the next line it must end with '/'

The ‘#’ character at the beginning of a line indicates that it is a comment and therefore the interpreter will not execute it

To display text or variables on the screen, use the echo command

Variables

Each shell has associated with some variables, that the user can use. There are two types of variables:

  • Local Variables: Are not inherited by the child processes of the shell when doing a fork
  • Environment Variables: Are inherited by the child process when a fork

All variables are preceded by ‘$’

Use of variables

To define a new variable or a value to a variable use the =operator

As we have mentioned before, to display on the screen the value of the variable use the echo command

To convert a local variable into an environment variable use the export command

To delete a variable and its value from memory, use the command unset

To display all variables (local and environment), we will use the set command

To show only environment variables, use the command env

We can use the following environment variables pre-defined within the shell

$HOME Home directory of the user
$PWD Working directory of the user
$PATH Absolute path of directories to search for executables. The working directory of the user is not included by default
$PS1 Prompt main shell
$PS2 Prompt secondary
$IFS Internal separator Field. Used by the command internal read

Arguments

Like any program, a script can receive values through arguments or parameters, in the command line. The arguments received are stored in a series of variables that the script can use

$1 The first argument
$2 The second argument
$3 Third argument
$4 Fourth argument
$5 Fifth argument
$6 Sixth argument
$7 The seventh argument
$8 Eighth argument
$9 Ninth argument
$C Name of the script
$* String containing all the arguments
$# The number of arguments received by the script
shift To move all arguments to the left position. That is to say, the value $1 disappears and is replaced by the $2, and so on

Quotes

Simple ‘ ‘

String of characters that the shell takes literally

Reverse ` `

Must enclose a complete command (with its name and its arguments) produces a run in three phases:

  1. Evaluation of variables
  2. Substitution of the command for a string that contains the standard output of the command
  3. Execution of the result

Dobles " "

Allow the evaluation of variables and command substitution

Commands flow control

Sentence if

expression can be a comparison expression or a command. Its value will be the one that returns the expression or the execution of a command

To enter by the branch that follows then, expression must have returned a 0 (true) and to enter an elif branch or an else branch, expression must have returned a value other than 0 (false)

Expressions

If we want to compare two values we must use the test

test evaluates the expression. If this is true it returns code 0 and if this is false a code other than 0

The most common expressions are:

-r file true if the file exists and has read permission
-w file true if the file exists and you have write permission
-x file true if the file exists and you have execute permission
-f file true if the file exists and is a file
-d file true if the file exists and is a directory
-s file true if the file exists and its size is greater than 0
-z string true if the file exists and its size is 0
-n string true if the file exists and its size is different from 0
string1 == string2 true if string1 is equal to string2
string1 != string2 true if string1 is different from string2
string true if string is not the null string
n1 -eq n2 true if n1 is equal to n2
n1 -ne n2 true if n1 is different than n2
n1 -gt n2 true if n1 is greater than n2
n1 -ge n2 true if n1 is greater or equal to n2
n1 -lt n2 true if n1 is less than n2
n1 -le n2 true if n1 is less than or equal to n2

These expressions elementals can be combined using parenthesis and the following operators:

! negates the expression
-to is equivalent to the AND operator
-or is equivalent to the OR operator

Case statement

Compares the string with the patterns, executing the list of commands corresponding to the first pattern with which it agrees, after which the execution of the case statement ends. A pattern can include any of the metacharacters of the shell

* is the default pattern and runs when none of the others

For statement

The list of commands is executed as many times as items are in the list. Before each iteration the next value of the list is assigned to variable, starting the first iteration by the first value

It is very useful if we want to go through all the arguments

Sentence while

command_list runs as many times as true expression

Judgment until

command_list is executed as many times as false expression

OR operator

command1 is executed; if it returns an error code other than 0 then the command2 is executed, and the code it returns is the code returned by the complete command; if command1 succeeds (code 0) then command2 is not executed and all expression succeeds

The previous example checks to see if the fich1 file exists. If it exists, nothing is done, otherwise a message is displayed by the standard error channel

AND operator

command1 is executed; if it returns an error code equal to 0 then the command2

In the example checks to see if there is a directory called dir1. If it exists, the content is displayed, otherwise nothing is done

Note: there is an operator object command

Read command

value_list are variables separated by a space

The execution of the read command reads from its standard input a line of text divided into words (i.e. strings separated by blanks or tabs)

Assigns the first word to the first variable, the second to the second variable, and so on until you complete the value_list

If not entered enough words, to the variables remaining to be assigned the null string

If in the line there are more words than variables, then it is assigned to the last variable a string consisting of all the words leftovers

Example of trace line to line of a file

Exit command

All command returns a code to notify the end if its execution has been correct or if it has appeared an error. If the execution has been correct, it returns error code 0, and if there has been any problem it is returned as a code any positive number

The files command returned at the end of a code error; the exact error code returned by the last command in the file that is running. To modify this situation by default we have to use the exit command

Where the optional argument n will be an integer greater than or equal to 0. If you do not specify an argument is equivalent to exit 0

When running the exit command, it immediately terminates the execution of the file command and is returned as an error code the integer n