for loop, while loop, break out of loop

for loop

General format:

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done

Write in one line:

for var in item1 item2 ... itemN; do command1; command2... done;

eg:

Requirement: sum the numbers from 1 to 100.

[root@dl-001 sbin]# vim sum.sh
#!/bin/bash
sum=0
for i in `seq 1 5`
do
  sum=$[sum+$i]
done
echo "$sum"

[root@localhost sbin]# sh sum.sh 
15

Requirements: document list loop

[root@dl-001 sbin]# vim for.sh
#!/bin/bash
dir=/usr/local/sbin/
for a in `ls $dir`
do
    if [ -d $a ]
    then
        echo $a
        ls $a
    fi
done
echo "No directory file!"

[root@dl-001 sbin]# sh -x for.sh 
+ dir=/usr/local/sbin/
++ ls /usr/local/sbin/
+ for a in '`ls $dir`'
+ '[' -d datefile.sh ']'
+ for a in '`ls $dir`'
+ '[' -d filetar.sh ']'
+ for a in '`ls $dir`'
+ '[' -d for.sh ']'
+ for a in '`ls $dir`'
+ '[' -d if2.sh ']'
+ for a in '`ls $dir`'
+ '[' -d sum.sh ']'
+ echo 'No directory file!'
No directory file!

for -- separator

[root@dl-001 dd]# touch 1.txt
[root@dl-001 dd]# touch 1\ 2.txt        //Here's the meaning of "escape". The created file is a 1 2.txt file
[root@dl-001 dd]# ls
1 2.txt  1.txt
[root@dl-001 dd]# for i in `ls ./` ; do echo $i ; done
1
2.txt
1.txt        //It can be seen that the for loop has split the 1 2.txt loop into 1, 2.txt

Note: for uses spaces or line breaks (carriage returns) as delimiters by default.

while Loop

Normal format:

while condition
do
    command
done

Simplified format:

while condition; do ;done

Infinite loop syntax format:

while :
do
    command
done

eg:

Requirement: when the system load is greater than 10, send email, and execute it every 30 seconds.

[root@dl-001 shell]# vim while.sh
#!/bin/bash
while :
do
  load=`w|head -1 |awk -F 'load average:' '{print $2}' |cut -d . -f1`
  if [ $load -gt 10 ]
  then
      top |mail -s "load is high: $load" abc@111.com
  fi
   sleep 30
done
#While ":" indicates a dead cycle, or it can be written as while true, which means "true"

#Attention: awk -F 'load average:' specify 'load average:' as the separator here, pay attention to the space after the colon
#If this space is not added, the filtered result will have a space. You need to filter the space here  


[root@dl-001 shell]# sh -x while.sh 
+ :
++ head -1
++ awk -F 'load average: ' '{print $2}'
++ cut -d. -f1
++ w
+ load=0
+ '[' 0 -gt 10 ']'
+ sleep 30
.
.
.

Note: if you do not stop the script manually, it will continue to loop (press Ctrl+c to end), which is used with screen in the actual environment.

Requirement: in interactive mode, the user enters a character to check whether it meets the conditions, such as: null, non numeric, numeric. Make judgments on the characters respectively, and then make different responses.

[root@dl-001 sbin]# vim while2.sh
#!/bin/bash
while true
do
  read -p "Please input a number:" n
  if [ -z "$n" ]
  then 
      echo "You need input some characters!"
      continue
  fi
  n1=`echo $n|sed 's/[-0-9]//g'`
  if [ -n "$n1" ]
  then
      echo "The character must be a number!"
      continue
  fi
  break
done
echo $n
#continue: interrupt the while cycle and start again;
#break: it means to jump out of this loop, that is, the end of the while loop

[root@dl-001 shell]# sh while2.sh 
Please input a number: 
You need input a character!
Please input a number:eee333
The character must be a number!
Please input a number:3
3

break out of loop

eg:

[root@dl-001 sbin]# vim break.sh
#!/bin/bash
for i in `seq 1 5`
do
  echo "$i"
  if [ $i -eq 3 ]
  then
  break
  fi
  echo "$i"
done
echo "Finished!"


[root@dl-001 sbin]# sh break.sh 
1
1
2
2
3
Finished!

That is, jump out of the while loop and continue to execute commands other than the loop.

Keywords: vim shell

Added by samafua on Thu, 30 Apr 2020 13:57:53 +0300