for shell script
DESCRIPTION
LInux - shell scripting - bucle forTRANSCRIPT
![Page 2: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/2.jpg)
while es una estructura que, en general, utilizamos cuando no sabemos de antemano, cuántas veces se va a repetir un código de programa
WHILE VS. FOR
![Page 3: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/3.jpg)
Usamos el bucle while:Cuando el usuario debe ingresar un número en particular para salir del menú
“Pulse 0 para salir”
WHILE VS. FOR
![Page 4: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/4.jpg)
Usamos el bucle while:Cuando la condición no es numérica
while [ $a != “salir” ] while [ $a == “true” ]
WHILE VS. FOR
![Page 5: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/5.jpg)
WHILE VS. FOR
for, en cambio, se utiliza, normalmente, cuando sabemos exactamente cuántas veces se tiene que repetir el ciclo
![Page 6: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/6.jpg)
WHILE VS. FOR
Usamos el bucle for:Cuando vamos desde un número hasta otro en secuencia
Desde 1 hasta 10 de uno en unoDesde 10 hasta 0 de dos en dos
![Page 7: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/7.jpg)
WHILE VS. FOR
Usamos el bucle for:Vamos a ver que en bash podemos utilizar otras cosas como contadores, por ejemplo listas de palabras
![Page 8: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/8.jpg)
WHILE VS. FOR
En cualquier caso, for se compone de una variable que va tomando distintos valores de una lista posible, uno por vez
![Page 9: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/9.jpg)
FOR
Sintaxis – versión 1for variable in {inicio..fin..intervalo}do
accionesdone
![Page 10: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/10.jpg)
FOR
for i in {10..0} #si no pongo intervalo
do #va de uno en uno
echo $i #hacia adelante o
sleep 1 #hacia atrás
done
![Page 11: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/11.jpg)
FOR
for i in {0..10..2}do echo "bienvenido $i veces"done
![Page 12: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/12.jpg)
FOR
Sintaxis – versión 2 (parecido a java)for ((inicio; condicion fin; incremento))do
sentenciasdone
![Page 13: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/13.jpg)
FOR
for ((i=0; i<10; i++)) # doble do # paréntesis echo $i done
![Page 14: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/14.jpg)
FOR
for ((i=0; i<11; i+=2))do echo "bienvenido $i veces"done
![Page 15: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/15.jpg)
FOR
Sintaxis – versión 3for variable in lista de valoresdo
sentenciasdone
![Page 16: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/16.jpg)
FOR
for i in 10 9 8 7 6 5 4 3 2 1 0 do # espacio entre cada caracter echo $i done
![Page 17: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/17.jpg)
FOR
for 0 2 4 6 8 10do echo "bienvenido $i veces"done
![Page 18: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/18.jpg)
FOR
Esta última sintaxis es más interesante porque la lista puede ser no solo de números, sino también de caracteres, archivos, nombres, etc.
![Page 19: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/19.jpg)
FOR
lista=`ls /bin/c*`for i in $listado
echo “archivo: $i”done
![Page 20: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/20.jpg)
FOR
Ejercicio:Pedir al usuario la cantidad de palabras que quiera ingresar en un archivo. Luego debemos leer el archivo e ir imprimiendo las palabras de a una por vez
![Page 21: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/21.jpg)
FOR
Ejercicio:Crear un script que le permita ingresar tantos nombres como se quiera y que al presionar una tecla termine su ejecución. Los nombres deberán quedar guardados en un archivo llamado“nombres.txt”.
![Page 22: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/22.jpg)
FOR
Ejercicio:
Crear un script similar al del punto anterior pero en lugar de presionar una tecla para finalizar, el usuario debe especificar de antemano cuantos nombres quiere cargar.
![Page 23: For shell script](https://reader034.vdocuments.net/reader034/viewer/2022052214/557d3c20d8b42ac2788b4ec1/html5/thumbnails/23.jpg)
FOR
Ejercicio:
Crear un shell scipt que muestre por pantalla los nombres guardados en el archivo “nombres.txt”, pero de a uno y limpiando la pantalla antes de mostrar el siguiente nombre.