Shell programming (3) - for loop, while loop, break, continue, exit

[tap]

1. For loop

Repeating a series of commands is common in programming. Often you need to repeat a set of commands until a certain condition is reached, such as processing all files in a directory, all users on the system, or all lines in a text file.

Two common loops are commonly used in scripts.

for loop
while loop

Syntax: for variable name in condition; do …; done

for var in list
do
  commands
done

在list参数中,提供了迭代中要用的一系列值

Example 1: Use a for loop to write a sum of 1-100.

Ideas:

  • [ ] First need to loop 1-100
  • [ ] Loop adds one at a time and assigns it to sum
  • [ ] output the value of sum
  • seq This is to traverse the numbers 1-100.
[root@xavi ~]# !vim
vim sum01.sh

#!/bin/bash
sum=0
for i in `seq 1 100`
do
   sum=$[ $sum+$i ]
   echo $i
done
echo $sum

Example 2: Traverse a directory's directories or files

#!/bin/bash
cd /etc/            //脚本中如果查询的目录在多级目录下,首先要进入到目录,或者,直接跟绝对路径
for a in `ls /etc/`     //遍历/etc目录
do
    if [ -d $a ]        //一个一个的去判断是否为目录
    then
    ls -d $a    //如果是目录,就查看下目录内的文件+子目录
    fi
done
[root@xavi ~]# !vim
vim for02.sh

#!/bin/bash
cd /etc/
for a in `ls /etc/`
do
   [ -d $a ] && ls $a
 # 判断是否是目录,并列出其下文件和子目录
   if [ -d $a ]
   then
       echo $a
       ls $a
   fi
done
~             

Special for loop example: when the list loops, spaces or carriage returns are used as separators

[root@xavi ~]# mkdir xavi
[root@xavi ~]# cd xavi/
[root@xavi xavi]# ls
[root@xavi xavi]# touch 1 2
[root@xavi xavi]# touch 3\ 4.txt
[root@xavi xavi]# ls
1  2  3 4.txt
[root@xavi xavi]# for i in `ls ./`; do echo $i ; done
1
2
3
4.txt //把3 4.txt一个文件拆分成两个了

2. while loop

grammar:

while test command
do
  other commands
  done

Case 1: Write a script to monitor system load

#!/bin/bash
while :         //:为死循环的意思,也相当于ture,
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" [email protected]
fi
sleep 30
// 负载有时候不可能一秒钟就起来了,所以每隔30秒判断一次即可。我们使用sleep 去暂停30秒钟。然后再次执行。。。
done

Case 2: Get a value through interaction with the user, and judge.

Output the number entered by the user.

  • [ ] First judge, what if the user does not input?
  • [ ] What if the user input is not a pure number?
  • [ ] According to the number entered by the user, and output.
#!/bin/bash
while :
do
    read -p "Please input a number: " n
    if [ -z "$n" ]
    then
        echo "you need input sth."
        continue
    fi
    n1=`echo $n|sed 's/[0-9]//g'`
    if [ -n "$n1" ]  // -n = ! -z ,记得加双引号
    then
        echo "you just only input numbers."
        continue
    fi
    break
done
echo $n
  • [ ] continue: When the user has no input, first prompt, and then continue to let the user input. Until the input is a real number, break out of the if statement.

  • [ ] break: If it is a real number, break out of the entire while statement.

Third, the usage of break

Example:

#! /bin/bash

for i in `seq 1 5`
do 
    echo $i
if [ $i == 3 ]
then
    break
fi
echo $i
done
echo aaa

The execution steps are as follows:

[root@xavi xavi]# sh -x break.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ echo 1
1
+ '[' 1 == 3 ']'
+ echo 1
1
+ for i in '`seq 1 5`'
+ echo 2
2
+ '[' 2 == 3 ']'
+ echo 2
2
+ for i in '`seq 1 5`'
+ echo 3
3
+ '[' 3 == 3 ']'
+ break
+ echo aaaa
aaaa

That is to say, once a break is encountered, it will jump out of the current loop and proceed to the next step.

Fourth, the usage of continue:

Ignore the code under continue and proceed directly to the next loop.

#! /bin/bash

for i in `seq 1 5`
do 
    echo $i
if [ $i == 3 ]
then
    continue
fi
echo $i
done
echo aaa

The execution result is as follows:

1
1
2
2
3
4
4
5
5
aaa

That is to say, when the execution reaches continue, any of the following statements are ignored directly. . Go directly to the next for loop.

Five, exit to exit the entire script

#! /bin/bash

for i in `seq 1 5`
do 
    echo $i
if [ $i == 3 ]
then
    exit
fi
echo $i
done
echo aaa

The execution steps are as follows:

++ seq 1 5
+ for i in '`seq 1 5`'
+ echo 1
1
+ '[' 1 == 3 ']'
+ echo 1
1
+ for i in '`seq 1 5`'
+ echo 2
2
+ '[' 2 == 3 ']'
+ echo 2
2
+ for i in '`seq 1 5`'
+ echo 3
3
+ '[' 3 == 3 ']'
+ exit
  • [ ] When going straight to 3, exit the script directly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324605021&siteId=291194637