1. The writing method of for statement based on C language format:
for((i=0;i<10;i++)); do【for (initial conditions ; Conditional judgment statement; Value change statement); do】
loop body
done
The advantage of for loop: it is not easy to enter an infinite loop
2, the format of the while statement:
initial value
While condition; do
loop body
Let i++ (change the initial value)
Done
Exercise: write a for loop to traverse all up computers in this network segment
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#!/bin/bash declare -i sum =0 for i in $( seq 1 100); do
|
Find the even sum of 1-100
1
2
3
4
5
6
7
|
#!/bin/bash for i in $( seq 0 2 100); do
|
Print the nine-nine multiplication table
1
< div class="line number2 index1 alt1"> 2 3
4
5
6
|
< div class="container">
for i in {1..9}; do
|
1
2 < /div>
3
4
5
6
7
8
9
10
11
12
13
|
#!/bin/bash declare -i sum =0 for i < code class="bash keyword">in $( seq 1 100); do
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#! /bin/bash declare
|
1< /p>
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash
declare
-i
sum
=0
for
i
in
$(
seq
1 100);
do
/code> ping
-c 1 -w 1 10.6.12.$i &>
/dev/null
if
[ $? -
eq
0 ];
then
let
sum
++
echo
< code class="bash string">"10.6.12.$i是通的"
else
echo
"10.6.12.$i is not working"
fi
done
echo
" in total $sum computers online"
#!/bin/bash
p>
declare
-i
sum
=0
for
i
in
$(
seq
1 100);
do
ping
-c 1 -w 1 10.6.12.$i &>
/dev /null
if
[ $? -
eq
0 ];
then
let
sum
< code class="bash plain">++
echo
"10.6.12.$i是通的"
else
echo
" 10.6.12. $i is not working"
fi
p>
done
echo
"A total of $sum computers are online"
1
2
3
4
5
6
7
#!/bin/bash
for
i
in
$(
seq
code> 0 2 100);
do
< code class="bash spaces"> let
sum
+ =$i
echo
< code class="bash plain">$i
done
echo
$
sum
1
2
3
4
5
6
7
#!/bin/bash
for
i
in
< code class="bash plain">$( seq
0 2 100);
do
let
code> sum
+=$i
echo
$i
done
echo
$
sum
1
2
3
4
5
6< /p>
7
#!/bin/bash
< div class="line number2 index1 alt1">