for loop of shell - while loop - break continue exit

Posted by egalp on Mon, 30 Dec 2019 21:39:41 +0100

for cycle:

Syntax:

for variable name in condition

do

command

done

#for follows the set of conditions, with a space as the separator to divide each condition.

 

Example:

#!/bin/bash
#Sum 1 of 100
sum=0
for in in `seq 1 100`
do 
sum=$[$sum+$i]
done
echo $sum
#!/bin/bash
#Find the directory in / etc / directory and display it in long format
cd /etc
for i in `ls /etc/`
do
[ -d $i ] && ls -d $i
done

 

while loop:

Syntax:

while loop condition

 do

command

done

#When the loop condition is 1 or true or ":", it means a dead loop.

 

Example:

#!/bin/bash
#Monitor the system load every 30S. When the load is greater than 10, send mail. Suppose the mail script is in / usr/local/sbin/mail.py
while true
do
load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d "." -f 1`
[ $load -gt 10 ] && /usr/local/sbin/mail.py "load high" "$load"
sleep 30 
done
#!/bin/bash
#Let the user input a number. If no character is input, prompt the user to input; if the user input a non number, prompt the user to input a number; until the number is input, exit cycling
while :
do
    read -p "Please input a number: " n
    if [ -z "$n" ]
    then
        echo "you need input number"
        continue
#After executing continue, jump out of this cycle, do not execute next cycle
    fi
    n1=`echo $n|sed 's/[0-9]//g'`
    if [ -n "$n1" ]
    then
        echo "you need input numbers."
        continue
    fi
    break
#After performing a break, exit this while loop
done
echo $n

 

break out of loop:

Often used in loop statements to jump out of the loop and end the loop

Example:

#!/bin/bash
for i in `seq 1 5`
do
  if [ $i -eq 3 ]  
  then
      break
  else
      echo $i
  fi
done
echo aaaaa

Result:

#sh break.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ '[' 1 -eq 3 ']'
+ echo 1
1
+ for i in '`seq 1 5`'
+ '[' 2 -eq 3 ']'
+ echo 2
2
+ for i in '`seq 1 5`'
+ '[' 3 -eq 3 ']'
+ break
+ echo aaaaa
aaaaa

 

continue to end this cycle:

It is often used in loop statements. It does not execute the statement after continue and directly starts the next cycle

#!/bin/bash
for i in `seq 1 5`
do
  if [ $i -eq 3 ]
  then
      continue
  else
      echo $i
  fi
done
echo aaaaa

Result:

#sh continue.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ '[' 1 -eq 3 ']'
+ echo 1
1
+ for i in '`seq 1 5`'
+ '[' 2 -eq 3 ']'
+ echo 2
2
+ for i in '`seq 1 5`'
+ '[' 3 -eq 3 ']'
+ continue
+ for i in '`seq 1 5`'
+ '[' 4 -eq 3 ']'
+ echo 4
4
+ for i in '`seq 1 5`'
+ '[' 5 -eq 3 ']'
+ echo 5
5
+ echo aaaaa
aaaaa

 

Exit exit the entire script:

When used in a script, exit the script directly and define the return value when exiting

#!/bin/bash
for i in `seq 1 5`
do
  if [ $i -eq 3 ]
  then
      exit 2
  else
      echo $i
  fi
done
echo aaaaa

Result:

#sh exit.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ '[' 1 -eq 3 ']'
+ echo 1
1
+ for i in '`seq 1 5`'
+ '[' 2 -eq 3 ']'
+ echo 2
2
+ for i in '`seq 1 5`'
+ '[' 3 -eq 3 ']'
+ exit 2


#echo $?
2