Shell script – loop

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
/code> ping -c 1 -w 1 10.6.12.$i &> /dev/null
if< /code> [ $? - eq 0 ]; the n
let sum ++
echo "10.6.12.$i是通的"
else
< code class="bash spaces"> echo "10.6.12.$i is not working"
fi
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
let sum +=$i
echo $i
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
for ((j=1 ;j<=$i;j++)); do
echo -n -e " $i*$j=$(($i*$j))\t"
done
echo
done

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
ping -c 1 -w 1 10.6.12.$i &> /dev/null
if [ $? - eq 0 ]; then
let sum code> ++
echo "10.6.12.$i是通的"
else
else

/code> echo "10.6.12.$i is not working"

fi
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> -i sum =0

for i in< /code> $( seq 1 100); do
ping -c 1 -w 1 10.6.12.$i &> /dev/null
if [ $? - eq 0 ]; then
let sum ++
echo "10.6.12.$i是通的"
else
echo "10.6.12.$i is not working"

< div class="line number11 index10 alt2"> fi

done
echo "There are a total of $sum computers online"

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">

for i< /code> in $( seq 0 2 100); do
let sum +=$i
echo $i
done
echo $ sum

#!/bin/bash

for < code class="bash plain">i in $( seq 0 2 100); do

let sum +=$i< /code>

echo $i code>

done

echo $ sum

1
2
3
4
5
6
for i in {1..9}; do
for ((j=1;j<=$i;j++)); do
echo -n -e "$i*$j=$(($i*$j))\ t"
done

echo
done

1

2
3
4
5

6
< code class="bash keyword">for i in {1..9}; do
/code> for ((j=1;j<=$i;j++)); do
echo code> -n -e "$i*$j=$(($i*$j))\t"
done
echo
done

1

2

3

4

5

6

for i in {1..9}; do
for  ((j=1;j<=$i;j ++)); do
                 echo  -n -e  "$i*$j=$(($i*$j))\t"
         done
         echo
done

for  in  {1..9}; do

         for  ((j=1;j<=$i;j++)); d o

                 echo  -n -e  "$i*$j=$(($i*$j))\t"

         done

         echo

done

Leave a Comment

Your email address will not be published.