-
-
Notifications
You must be signed in to change notification settings - Fork 525
Translate to spanish 015-writing-your-first-bash-script.md (added recently 016,017 and 018 too) #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Translate to spanish 015-writing-your-first-bash-script.md (added recently 016,017 and 018 too) #128
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
109f19b
Translate to spanish 015-writing-your-first-bash-script.md
BlackeyeB 0167077
Translate to spanish 015-writing-your-first-bash-script.md
BlackeyeB ef7a1a6
Translated to spanish 3 files (016,017 and 018)
BlackeyeB 3aaa573
Merge pull request #1 from BlackeyeB/translations_es
BlackeyeB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
180 changes: 180 additions & 0 deletions
ebook/es/content/015-writing-your-first-bash-script.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| # Escribe tu primer script en Bash | ||
|
|
||
| Vamos a intentar agrupar lo que hemos aprendido hasta ahora y crear nuestro primer script en Bash | ||
|
|
||
| ## Planeando el script | ||
|
|
||
| Como ejemplo, escribiremos un script que debería recopilar alguna información útil sobre nuestro servidor como: | ||
|
|
||
| * Uso del disco actual | ||
| * Uso de la CPU actual | ||
| * Uso de la RAM actual | ||
| * Controlar la versión exacta del kernel | ||
|
|
||
| Siéntete libre de ajustar el script añadiendo o borrando las funcionalidades según tus necesidades. | ||
|
|
||
| ## Escribiendo el script | ||
|
|
||
| Lo primero que necesitas hacer es crear un nuevo archivo con una extensión `.sh`. Yo crearé un archivo llamado `status.sh` ya que el script que crearemos nos dará el estado de nuestro servidor. | ||
|
|
||
| Una vez hayas creado el archivo, ábrelo con tu editor de texto favorito. | ||
|
|
||
| Como hemos aprendido en el capítulo 1, en la primera linea de nuestro script en Bash necesitamos especificar el llamado [Shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)): | ||
|
|
||
| ```bash | ||
| #!/bin/bash | ||
| ``` | ||
|
|
||
| El shebang hace dice al sistema operativo que ejecute el script con el ejecutable /bin/bash. | ||
|
|
||
| ## Añadiendo comentarios | ||
|
|
||
| A continuación, como vimos en el capítulo 6, vamos a empezar añadiendo algunos comentarios para que la gente pueda entender fácilmente para que sirve el script. Para hacerlo de manera correcta, después del shebang, puedes añadir lo siguiente: | ||
|
|
||
| ```bash | ||
| #!/bin/bash | ||
|
|
||
| # Script que devuelve el estado actual del servidor | ||
| ``` | ||
|
|
||
| ## Añadiendo tu primera variable | ||
|
|
||
| Ahora sigamos avanzando y apliquemos lo que hemos aprendido en el capítulo 4 añadiendo algunas variables que podríamos utilizar en todo el script. | ||
|
|
||
| Para asignar un valor a una variable en bash, sólo tienes que usar el símbolo `=`. Por ejemplo, vamos a almacenar el nombre de nuestro servidor en una variable que podremos usar más adelante. | ||
|
|
||
| ```bash | ||
| server_name=$(hostname) | ||
| ``` | ||
|
|
||
| Usando `$()`le decimos a bash que interprete el comando y luego le asigne el valor a nuestra variable. | ||
|
|
||
| Ahora si hacemos un `echo` de la variable veriamos el nombre actual del servidor. | ||
|
|
||
| ```bash | ||
| echo $server_name | ||
| ``` | ||
|
|
||
| ## Añadiendo tu primera función | ||
|
|
||
| Como ya sabrás después de leer el capítulo 12, para crear una función en bash necesitas usar la siguiente estructura: | ||
|
|
||
| ```bash | ||
| function function_name() { | ||
| your_commands | ||
| } | ||
| ``` | ||
|
|
||
| Vamos a crear una función que devuelva el uso actual de la memoria en nuestro servidor: | ||
|
|
||
| ```bash | ||
| function memory_check() { | ||
| echo "" | ||
| echo "The current memory usage on ${server_name} is: " | ||
| free -h | ||
| echo "" | ||
| } | ||
| ``` | ||
|
|
||
| Una vista rápida de la función: | ||
|
|
||
| * `function memory_check() {` - Esto es como definimos la función | ||
| * `echo ""` - Aquí imprimimos una nueva línea | ||
| * `echo "The current memory usage on ${server_name} is: "` - aquí imprimimos un pequeño mensaje y la variable `$server_name` | ||
| * `}` - finalmente así es como cerramos la función. | ||
|
|
||
| Una vez la función ha sido definida, para llamarla, sólo tenemos que usar el nombre de la función: | ||
|
|
||
| ```bash | ||
| # Define la función | ||
| function memory_check() { | ||
| echo "" | ||
| echo "The current memory usage on ${server_name} is: " | ||
| free -h | ||
| echo "" | ||
| } | ||
|
|
||
| # Llama a la función | ||
| memory_check | ||
| ``` | ||
|
|
||
| ## El desafío de agregar más funciones | ||
|
|
||
| Antes de comprobar la solución, te desafío a usar la función de arriba y escribir algunas funciones por tí mismo. | ||
|
|
||
| Las funciones deberían hacer lo siguiente: | ||
|
|
||
| * Uso actual del disco | ||
| * Uso actual de la CPU | ||
| * Uso actual de la RAM | ||
| * Controlar la versión exacta del kernel | ||
|
|
||
| Siéntete libre de usar google para obtener esa información si no estás seguro de los comandos que necesitas usar. | ||
|
|
||
| Una vez que termines, siéntete libre de seguir leyendo y comprobar como lo hemos hecho para comparar los resultados. | ||
|
|
||
| Ten en cuenta que hay múltiples maneras correctas de hacerlo. | ||
|
|
||
| ## El script de ejemplo | ||
|
|
||
| Así es como se vería el resultado final: | ||
|
|
||
| ```bash | ||
| #!/bin/bash | ||
|
|
||
| ## | ||
| # script en BASH que comprueba: | ||
| # - Uso de memoria | ||
| # - Carga de CPU | ||
| # - Número de conexiones TCP | ||
| # - Versión de kernel | ||
| ## | ||
|
|
||
| server_name=$(hostname) | ||
|
|
||
| function memory_check() { | ||
| echo "" | ||
| echo "Memory usage on ${server_name} is: " | ||
| free -h | ||
| echo "" | ||
| } | ||
|
|
||
| function cpu_check() { | ||
| echo "" | ||
| echo "CPU load on ${server_name} is: " | ||
| echo "" | ||
| uptime | ||
| echo "" | ||
| } | ||
|
|
||
| function tcp_check() { | ||
| echo "" | ||
| echo "TCP connections on ${server_name}: " | ||
| echo "" | ||
| cat /proc/net/tcp | wc -l | ||
| echo "" | ||
| } | ||
|
|
||
| function kernel_check() { | ||
| echo "" | ||
| echo "Kernel version on ${server_name} is: " | ||
| echo "" | ||
| uname -r | ||
| echo "" | ||
| } | ||
|
|
||
| function all_checks() { | ||
| memory_check | ||
| cpu_check | ||
| tcp_check | ||
| kernel_check | ||
| } | ||
|
|
||
| all_checks | ||
| ``` | ||
|
|
||
| ## Conclusión | ||
|
|
||
| Hacer scripts en Bash es muy útil! No importa si eres un ingeniero, desarrollador o sólo un entusiasta de Linux, puedes usar los scripts en Bash para combinar diferentes comandos de Linux y automatizar tareas diarias, aburridas y repetitivas para poder enfocarte en cosas más productivas y divertidas. | ||
|
|
||
| >{aviso} Esto fue inicialmente publicado en [DevDojo.com](https://devdojo.com/bobbyiliev/introduction-to-bash-scripting) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.