for loop, while loop, break jumps out of the loop, continue ends the cycle, exit exits the entire script

Keywords: shell Hibernate

for cycle

  • Syntax: for variable name in condition; do... ; done
  • Case 1
    • 1+2+3. +100 and
    • When sum is first used as a variable, it is 0; when it enters the for loop, every operation changes the sum variable until the end of $i; the final output is $sum.
    • In addition and subtraction, [] there is no need to add spaces in square brackets
 #!/bin/bash
sum=0
for i in `seq 1 100`
#Seq 1 to 100 numbers
do
    sum=$[$sum+$i]
    echo $i
done
echo $sum
  • for loop case 2
    • File List Loop
    • List subdirectories under all etc directories
 #!/bin/bash
cd /etc/
for a in `ls /etc/`
do
    if [ -d $a ]
    then
       ls -d $a
    fi
done
[root@123 shell]# for a in `seq 1 3`; do echo $a; done
1
2
3

[root@123 shell]# for a in 1 2 3; do echo $a; done
1
2
3
[root@123 shell]# 
  • for loops, spaces or carriage returns are used as separators

 

 

while Loop

  • The grammatical while condition; do uuuuuuuuuuuu ; done
  • Case 1
    • Check the system load every 1 minute and send an email (monitoring script) when the system load is greater than 10.
      • The smallest unit is the task plan cron
 #!/bin/bash
while :
# Colon: It means a dead cycle, or 1, or true, is a dead cycle.
do
    load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`
    if [ $load -gt 10 ]
    then
           /usr/local/sbin/mail.py xxx@163.com "load high" "$load"
    fi
    sleep 30
#Hibernate for 30 seconds, because checking system load does not need to be checked all the time. Look at it later.
done
[root@123 shell]# sh -x while1.sh 
+ :
++ w
++ head -1
++ awk -F 'load average: ' '{print $2}'
++ cut -d. -f1
+ load=0
+ '[' 0 -gt 10 ']'
+ sleep 30
//If you want this script to terminate unexpectedly, you can open the screen and run the script in the screen.
  • Knowledge points

    • w// View system load;
    • uptime can directly display the first line of w system load, and head-1 can be omitted.
    • Head-1// Take the first line
    • Awk-F'load average:''{print $2}'// separated by'load average:', output the second paragraph
    • Cut-d. - F1 // / Separate the first paragraph
  • while loop case 2

    • During the cycle, the user is required to input a number; the input is not a number, but a number, and the input is empty; the corresponding results are responded to.
#!/bin/bash
while :
do
    read -p "Please input a number: " n
    if [ -z "$n" ]
    then
        echo "you need input sth."
        continue
#continue goes back to the loop
    fi
    n1=`echo $n|sed 's/[0-9]//g'`
    if [ -n "$n1" ]
    then
        echo "you just only input numbers."
        continue
    fi
    break
#break out of the loop
done
echo $n

 

 

 

break out of the loop

  • break is often used in circular statements
    • Jump out of the whole loop statement and end all loops directly
#!/bin/bash
for i in `seq 1 5`
do
    echo $i
    if [ $i -eq 3 ]
#Compare numbers with - eq; if you compare strings, you need to use==
    then
        break
    fi
    echo $i
done
echo aaaaaaa
[root@123 shell]# sh -x berak.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ echo 1
1
+ '[' 1 -eq 3 ']'
+ echo 1
1
+ for i in '`seq 1 5`'
+ echo 2
2
+ '[' 2 -eq 3 ']'
+ echo 2
2
+ for i in '`seq 1 5`'
+ echo 3
3
+ '[' 3 -eq 3 ']'
+ break
+ echo hahaha
hahaha
[root@123 shell]# sh berak.sh
1
1
2
2
3
hahaha
[root@123 shell]# 

 

 

continue ends this cycle

  • Ignore the code under continue and go straight to the next loop
#!/bin/bash
for i in `seq 1 5`
do
    echo $i
    if [ $i -eq 3 ]
    then
        continue
    fi
    echo $i
done
echo $i
[root@123 shell]# sh continue.sh
1
1
2
2
3
4
4
5
5
hahaha
[root@123 shell]# 

 

 

exit exits the entire script

  • exit ends exiting the entire script directly
    • You can define exit values that can be used to determine where the script runs and when it ends
#!/bin/bash
for i in `seq 1 5`
do
    echo $i
    if [ $i -eq 3 ]
    then
        exit
    fi
    echo $i
done
echo aaaaaaa
[root@123 shell]# sh exit.sh
1
1
2
2
3
[root@123 shell]# 

 

extend
select usage http://www.apelearn.com/bbs/thread-7950-1-1.html

 

Posted by coditoergosum on Sat, 15 Dec 2018 22:51:03 -0800