-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path7) Loops.sh
142 lines (108 loc) · 3.56 KB
/
7) Loops.sh
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
===========================================
Formato while loop:
#!/bin/bash
echo "type one value"
read X
echo "type the limit value"
read Y
while [ $X -lt $Y ]; do
sum=$((X+1))
echo "$sum"
X=$((X+1))
done
#!/bin/bash
while true
do
echo "oia"
sleep 1
done
===========================================
Formato for loop:
((inicio; condição; passo))
#!/bin/bash
echo "Enter a starting value:"
read X
echo "Enter the limit value:"
read Y
for ((; X < Y; X++)); do
echo -e ""
echo "$X"
if [ "$X" == 22 ]; then
break
fi
done
ou
#!/bin/bash
frutas=("maçã" "uva" "banana" "laranja")
#echo "${frutas[@]}"
for frutos in "${frutas[*]}";do
echo "$frutos"
done
ou
#!/bin/bash
for c in {0..10}
do
echo "C value is $c"
done
Uso do continue no for
#!/bin/bash
X=1
Y=10
for ((; X <= Y; X++)); do
if [ "$X" == 5 ]; then
continue
fi
echo "$X"
done
ou
#/bin/bash
for a in 1 2 3 4 5 6 7 8 9 10
do
# if a = 5 then continue the loop and
# don’t move to line 8
if [ $a == 5 ]; then
continue
fi
echo “Iteration no $a”
done
===========================================
Until loop :O loop until é usado para repetir um bloco de comandos até que uma condição se torne verdadeira.
contador=0
until [ $contador -eq 5 ]
do
echo "Contador: $contador"
contador=$((contador + 1))
done
======================================================================================
EXEMPLOS E FINALIZAÇÃO:
#!/bin/bash
verification='y'
while true
do
# Solicita ao usuário que insira o nome
read -p "Enter your name: " NAME
# Pergunta ao usuário se o nome fornecido está correto
read -p "Is ${NAME} correct? (y/n) " verification
# Verifica se a resposta é 'n' e exibe uma mensagem
if [ "$verification" == 'n' ]; then
echo "Try again."
else
break
fi
done
+----+-----------------------------------------------------+----------------------------------------------------+
| | break | continue |
+----+-----------------------------------------------------+----------------------------------------------------+
| 1 | ==== It terminates the execution of the loop for | ==== It skips the execution of the loop for only |
| | all the remaining iterations. | the current iteration. |
+----+-----------------------------------------------------+----------------------------------------------------+
| 2 | ==== It allows early termination of the loop. | ==== It allows early execution of the next |
| | | iteration. |
+----+-----------------------------------------------------+----------------------------------------------------+
| 3 | ==== It stops the execution of loops. | ==== It stops the execution of the loop only for |
| | | the current iteration. |
+----+-----------------------------------------------------+----------------------------------------------------+
| 4 | ==== The code after the loop which was terminated | ==== The code in the loop continues its execution |
| | is continued. | skipping the current iteration. |
+----+-----------------------------------------------------+----------------------------------------------------+
======================================================================================