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 echo "10.6.12.$i是通的" < code class="bash spaces">
echo "10.6.12.$i is not working" done echo "There are a total of $sum computers online" |
Find the even sum of 1-100
1
2
3
4
5
6
7
|
#!/bin/bash for i in $( seq 0 2 100); do done echo $ sum |
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 done |
1
2 < /div>
3
4
5
6
7
8 div>
9
10
11
12
13
|
#!/bin/bash div>
declare -i sum =0 for i < code class="bash keyword">in $( seq 1 100); do done echo "There are a total of $sum computers online" |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#! /bin/bash declare code> for i in< /code> "10.6.12.$i是通的" < div class="line number11 index10 alt2"> |
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 div>
3
4
5
6
7
|
#!/bin/bash div>
for i in $( seq code> 0 2 100); do < code class="bash spaces">
let sum + =$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 done echo $ sum |
1
2
3
4
5
6< /p>
7
#!/bin/bash
< div class="line number2 index1 alt1">