Linux/Unix Shell Scripting - Ayuda con ejercicio sobre READ

 
Vista:
sin imagen de perfil
Val: 5
Ha aumentado 1 puesto en Linux/Unix Shell Scripting (en relación al último mes)
Gráfica de Linux/Unix Shell Scripting

Ayuda con ejercicio sobre READ

Publicado por paco01 (3 intervenciones) el 06/06/2017 12:21:17
Buenas. Pues lo que tengo que hacer es un programa simulando el programa de TV "Ahora caigo". Es decir, el script te hace una pregunta y tú tienes 20 segundos para responderla y si no aciertas pasa a la siguiente (además te resta puntos). Mi problema está en esos 20 segundos, pues cuando se pasan, me salta un error y el script se salta el resto de preguntas. Os paso el trozo de código donde se hacen 10 preguntas:

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
RESPUESTA=" "
PUNTOS=10
NOMBRE=" "
clear
 
##El scrip coge las preguntas de un fichero de texto
## Esta es la primera pregunta:
cat sets/set1 | head -n3 | tail -n1
read -t20 -p "Respuesta: " RESPUESTA
## Si el usuario no acierta o pasan 20 segundos, pasa a la siguiente, que es esta:
## Mi problema empieza cuando pasan los 20 segundos, pues el script se salta el resto
## de preguntas, y no sé porqué
if [ $RESPUESTA != "Lynx" ]; then
	clear
	cat sets/set1 | head -n4 | tail -n2
	PUNTOS=$[PUNTOS-1]
	echo "Tienes $PUNTOS puntos"
	read -t20 -p "Respuesta: " RESPUESTA
 
fi
if [ $RESPUESTA != "Lynx" ]; then
	clear
	cat sets/set1 | head -n5 | tail -n3
	PUNTOS=$[PUNTOS-1]
	echo "Tienes $PUNTOS puntos"
	read -t20 -p "Respuesta: " RESPUESTA
fi
if [ $RESPUESTA != "Lynx" ]; then
	clear
	cat sets/set1 | head -n6 | tail -n4
	PUNTOS=$[PUNTOS-1]
	echo "Tienes $PUNTOS puntos"
	read -t20 -p "Respuesta: " RESPUESTA
fi
if [ $RESPUESTA != "Lynx" ]; then
	clear
	cat sets/set1 | head -n7 | tail -n5
	PUNTOS=$[PUNTOS-1]
	echo "Tienes $PUNTOS puntos"
	read -t20 -p "Respuesta: " RESPUESTA
fi
if [ $RESPUESTA != "Lynx" ]; then
	clear
	cat sets/set1 | head -n8 | tail -n6
	PUNTOS=$[PUNTOS-1]
	echo "Tienes $PUNTOS puntos"
	read -t20 -p "Respuesta: " RESPUESTA
fi
if [ $RESPUESTA != "Lynx" ]; then
	clear
	cat sets/set1 | head -n9 | tail -n7
	PUNTOS=$[PUNTOS-1]
	echo "Tienes $PUNTOS puntos"
	read -t20 -p "Respuesta: " RESPUESTA
fi
if [ $RESPUESTA != "Lynx" ]; then
	clear
	cat sets/set1 | head -n10 | tail -n8
	PUNTOS=$[PUNTOS-1]
	echo "Tienes $PUNTOS puntos"
	read -t20 -p "Respuesta: " RESPUESTA
fi
if [ $RESPUESTA != "Lynx" ]; then
	clear
	cat sets/set1 | head -n11 | tail -n9
	PUNTOS=$[PUNTOS-1]
	echo "Tienes $PUNTOS puntos"
	read -t20 -p "Respuesta: " RESPUESTA
fi
if [ $RESPUESTA != "Lynx" ]; then
	clear
	cat sets/set1 | head -n12 | tail -n10
	PUNTOS=$[PUNTOS-1]
	echo "Tienes $PUNTOS puntos"
	read -t20 -p "Respuesta: " RESPUESTA
fi
 
if [ $RESPUESTA == "Lynx" ]; then
	echo "Enhorabuena! Has acertado con $PUNTOS puntos"
elif [ $PUNTOS -eq 1 ]; then
	echo "Mala suerte, has perdido"
fi
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Ayuda con ejercicio sobre READ

Publicado por Tom (361 intervenciones) el 06/06/2017 14:31:31
Probablemente la variable RESPUESTA está vacía. Si es así, además te estará dando errores en cada if (has "olvidado" comentarlo o mirarlo).
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 5
Ha aumentado 1 puesto en Linux/Unix Shell Scripting (en relación al último mes)
Gráfica de Linux/Unix Shell Scripting

Ayuda con ejercicio sobre READ

Publicado por paco01 (3 intervenciones) el 06/06/2017 17:52:23
Hola, gracias por responder. Estoy dándole vueltas a lo que me has dicho pero no se me ocurre cómo hacer que se cumplan los if si RESPUESTA está vacía. ¿Alguna sugerencia?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de [abZeroX]
Val: 101
Oro
Ha mantenido su posición en Linux/Unix Shell Scripting (en relación al último mes)
Gráfica de Linux/Unix Shell Scripting

Ayuda con ejercicio sobre READ

Publicado por [abZeroX] (27 intervenciones) el 07/06/2017 06:06:16
Hola, prueba cambiar
1
$RESPUESTA
por
1
"$RESPUESTA"
, debes replazarla en todas las secciones del código donde utilizas la condicional if.

Ejemplo:
1
2
3
4
5
6
7
if [ "$RESPUESTA" != "Lynx" ]; then
	clear
	cat sets/set1 | head -n4 | tail -n2
	PUNTOS=$[PUNTOS-1]
	echo "Tienes $PUNTOS puntos"
	read -t5 -p "Respuesta: " RESPUESTA
fi

Prueba con esto y nos comentas.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil
Val: 5
Ha aumentado 1 puesto en Linux/Unix Shell Scripting (en relación al último mes)
Gráfica de Linux/Unix Shell Scripting

Ayuda con ejercicio sobre READ

Publicado por ´paco01 (3 intervenciones) el 07/06/2017 14:18:17
Buenas. Era éso, lo he probado y ya me funciona a la perfección. Muchas gracias por toda la ayuda.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar