Os dejo un enlace mientras preparo el artículo:
if y comparaciones
http://www.dreamsyssoft.com/unix-shell-scripting/ifelse-tutorial.php
while
http://www.cyberciti.biz/faq/bash-while-loop/
case
http://bash.cyberciti.biz/guide/The_case_statement
Trabajar con variables:
http://www.dreamsyssoft.com/unix-shell-scripting/variables-tutorial.php
Ejemplo IF ELSE
1 2 3 4 5 6 7 8 9 10 11 12 13 | #!/bin/bash # This is some secure program that uses security. clear VALID_PASSWORD= "secret" #this is our password. echo "Please enter the password:" read PASSWORD if [ $PASSWORD == $VALID_PASSWORD ]; then echo "You have access!" else echo "ACCESS DENIED!" fi |
Ejemplo If Elif y comparaciones de números
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #!/bin/bash # This is some secure program that uses security. clear VALID_PASSWORD= "secret" #this is our password. echo "Please enter the password:" read PASSWORD if [ $PASSWORD == $VALID_PASSWORD ]; then echo "You have access!" elif [ $PASSWORD == "patata" ]; then echo "Te gustan las patatas fritas, verdad?!??!?!?!" echo "Pues no, no era esa la contraseña" elif [ $PASSWORD == "secreto" ]; then echo "UUUUYYYYY!!!!! AL PALOOOOO!!!!" else echo "Pues va a ser que no" fi if [ $PASSWORD != $VALID_PASSWORD ]; then echo "NOOOOOOORL" else echo "SIIIIIIIIIIIIIIIIIIIIIIIRLLLL" fi read "Pulse una tecla para continuar..." clear echo -n "Nombre: " ; read nombre echo -n "Edad: " ; read edad edad_profe=37 if [ $edad -lt 18 ]; then echo $nombre, a ver cuando creces! elif [ $edad - ge 18 ] && [ $edad -lt $edad_profe ]; then echo $nombre, ya eres mayor de edad else # edad mayor que la del profe echo $nombre, te estás haciendo mayor... fi |
Ejemplo Menu con While y Elif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #!/bin/bash # menu opcion=-1 while [ $opcion != 0 ]; do clear echo "1. Ver la hora" echo "2. Ver el usuario" echo "3. Mostrar un mensaje x veces" echo "0. Salir" echo -n "Elija una opción: " ; read opcion if [ $opcion == 1 ]; then date elif [ $opcion == 2 ]; then logname elif [ $opcion == 3 ]; then echo -n "¿Cuantas veces quieres repetir el mensaje? " ; read veces echo -n "¿Que mensaje quieres mostrar? " ; read mensaje x=1 while [ $x - le $veces ]; do clear echo $x. $mensaje x=$(($x+1)) done elif [ $opcion == 0 ]; then echo "Hasta luego Lucas!!!" else echo "Opción incorrecta" fi echo "Pulse una tecla para continuar" read done |
Ejemplo Menu con While y case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #!/bin/bash # menu opcion=-1 while [ $opcion != 0 ]; do clear echo "1. Ver la hora" echo "2. Ver el usuario" echo "3. Mostrar un mensaje x veces" echo "0. Salir" echo -n "Elija una opción: " ; read opcion case $opcion in 1) date ;; 2) logname ;; 3) echo -n "¿Cuantas veces quieres repetir el mensaje? " ; read veces echo -n "¿Que mensaje quieres mostrar? " ; read mensaje x=1 while [ $x - le $veces ]; do clear echo $x. $mensaje x=$(($x+1)) done ;; 0) echo "Hasta luego Lucas" ; exit ;; *) echo "Opción incorrecta" ;; esac echo "Pulse una tecla para continuar" read done |
Comentarios recientes