Tag Archive for: Bash

Tanto si eres un programador experimentado como si no, esta serie de publicaciones/artículos están destinados a todos los que deseen aprender a programar con el intérprete de shell de Unix/Linux.

 

Fundamentos shell de Unix/Linux

1. Lo basico y el comando “echo”

Comentarios, Que terminal utilizo?

El tutorial discute la programación de shells en general, centrándose en el shell Bash («Bourne Again Shell») como el principal intérprete de shell. También se hará referencia a la programación de shell utilizando otros shell comunes como sh, csh, tcsh, ya que a veces difieren de bash.

La programación de la shell puede realizarse ejecutando directamente los comandos de la shell en el prompt de la shell o almacenándolos en el orden de ejecución, en un archivo de texto, llamado shell script, y luego ejecutando el shell script. Para ejecutar, simplemente escriba el nombre del archivo del shell script, una vez que el archivo tenga permiso de ejecución (chmod +x nombre de archivo).

La primera línea del archivo de script de shell comienza con un «sha-bang» (#!) que no se lee como un comentario, seguido de la ruta completa donde se encuentra el intérprete de shell. Esta ruta, le indica al sistema operativo que este archivo es un conjunto de comandos que deben ser introducidos en el intérprete indicado. Tenga en cuenta que si la ruta dada en el «sha-bang» es incorrecta, entonces un mensaje de error, por ejemplo «Comando no encontrado», puede ser el resultado de la ejecución del script. Es habitual nombrar el script de shell con la extensión «.sh».

La primera línea puede tener el siguiente aspecto:

#!/bin/bash

Comentarios:
cualquier texto que siga a «#» se considera un comentario

Para saber cuál es el shell activo en ese momento y cuál es su ruta, escriba el comando resaltado en el prompt del shell (a continuación se muestran ejemplos de respuestas):

ps | grep $$

Esta respuesta muestra que el shell que está utilizando es de tipo ‘bash’. A continuación, averigüe la ruta completa del intérprete del shell

which bash

Esta respuesta muestra la ruta de ejecución completa del intérprete de shell. Asegúrese de que la línea «sha-bang» al principio de su script, coincide con esta misma ruta de ejecución.

Utilice el comando «echo» para imprimir la línea «¡Hola, mundo!».

echo ‘¡Hola, mundo!’

Version PDF:  shell-lo-basico


A Bash function is a collection of commands that can be executed repeatedly. The goal of the function is to make the Bash scripts easier to read and prevent you from typing the same script often. Bash functions are considerably constrained in comparison to those of the majority of programming languages. For step-by-step execution, this file contains various commands. Although these commands can be entered simply into the command line, it is more convenient to save all interconnected commands for a given operation in a single file from a reusability perspective. We can utilize that file to run the specified set of commands, a single time or multiple times, depending on our needs. We will go through the fundamentals of Bash functions in this lesson and demonstrate how to use them in shell scripts.

Example 1

A function is a section of code that can be called several times to carry out specific tasks in programming. Code length is decreased, and program modularity is provided. The most basic illustration of a function’s application in Bash scripting is shown in the following:

In the previous script, we have defined the function “BashFunction”. Inside it, we have an echo statement. Then, we called the function “BashFunction” to print.

The output of the Bash basic function is printed on the terminal:

Example 2

We can pass arguments and process data with Bash functions like in other programming languages. Similar to passing command line parameters to a Bash script, we can input data into the function. We must add the parameters immediately following the function name to provide any number of arguments to the Bash function. When calling the function, we can pass the arguments directly and use the values $1, $2,… $n to access them inside the function.

In the previous script, we have created the function “multiply_two_numbers”. In the function, we have created the variable “product” and assigned the equation of the product to be computed. The echo command is then used to print the outcome of the calculation for the product of two values. After that, we have provided the two arguments, 5 and 2, assigned to the function “multiply_two_numbers”.

The terminal shell after the script execution is shown in the following image:

Example 3

Most programming languages support the idea of functions returning a value. It implies that the function must return the data to the place from which it is called. Bash functions don’t support returning a value when they are called, unlike functions in vivo programming languages. However, they give us the option to establish a return status, which works similarly to the exit status used by a program or command to terminate.

The variable “$?” is allocated the word “return”, which can be employed to describe the return’s current state. The function is ended by the return statement, which also serves as the exit status for the function.

In the script, first, we have established a function “my_function”. In the function, we have a return keyword that takes the value “14”. Then, we invoked the function “my_function”, which has an echo statement that will return the value to “$”.

The Bash script output the value specified to a return command as follows:

Example 4

In a script, a integral variable is a variable that may be accessed from any location and has no restrictions on its scope. Even if a variable is declared inside a function, it is automatically defined as a integral variable. Variables can also be created locally. Merienda they have been allocated for the first time, lugar variables can be specified inside the function body using the keyword “lugar.” Only that function allows you to access them. The same name might be given to lugar variables created in other functions.


The Bash script begins with the declaration of the integral variable “var1” and initializes it with the value of a string. Then, we created the function “Animal_names_fun”. To the function “Animal_names_fun”, we have set the lugar variable with the keyword lugar and one integral variable. The lugar and integral variables have been defined with the names “var2” and “var3,” and we have also set their values. Then, we echoed the integral variable “var1” within the function and the lugar variable “var2” within the function. After that, we called the function “Animal_names_fun” and accessed the integral variables “var1” and “var3”, which usually execute, but as we are trying to access the lugar variable “val2” outside the specified function, it will return an empty value.

You can notify the output of accessing the lugar and integral variables in the bash program as follows:

Example 5

By naming a function the same as the command we want to override, we can choose to override the Bash commands. For instance, we must write a function called “echo” if we intend to override the echo command.

This idea of overriding Bash commands may be helpful in some circumstances, such as when we want to utilize a command with particular options. Additionally, there are situations when we do not want to repeat the entire command with options within the script. We can use options to alter the built-in Bash command in these situations. Let’s use an example to understand better the idea of overriding commands in Bash Shell Scripting:

In the previous Bash script, we have a function echo. Within the echo function, we overrode the echo command and inserted the built-in timestamp method as an argument to the echo command.

The output of the Bash override function is displayed in the following image:

Conclusion

We discussed the Bash function on this topic. A chunk of reusable code explicitly created for a given task is called a Bash function. Merienda defined, it can be used several times in a script. Bash engages with the operating system to carry out commands received from the shell after reading them. One fantastic feature of Bash is that it allows you to combine a lot of its commands and functions into a single executable script, which makes it easier to organize your work.



Source link