[TOC]
One, for loop
Repeated execution of a series of commands is common in programming. Usually you need to repeat a set of commands until you reach a certain condition, such as working with all the files in a directory, all users on the system, or all the lines in a text file.
There are two common loops that are commonly used in scripts.
For loop
While loop
Syntax: for variable name in condition; Do ...; Done
for var in listdo commandsdone在list参数中,提供了迭代中要用的一系列值
Example 1: Use a For loop to write a sum of 1-100.
Ideas:
- [] First you need to put 1-100 loops
- [] The loop is added once and assigned to sum
- [] outputs the value of sum
- Seq This is the traversal of 1-100 of these numbers.
[[email protected] ~]# !vimvim sum01.sh#!/bin/bashsum=0for i in `seq 1 100`do sum=$[ $sum+$i ] echo $idoneecho $sum
Example 2: Traversing directories or files of a directory
#!/bin/bashcd /etc/ //脚本中如果查询的目录在多级目录下,首先要进入到目录,或者,直接跟绝对路径for a in `ls /etc/` //遍历/etc目录do if [ -d $a ] //一个一个的去判断是否为目录 then ls -d $a //如果是目录,就查看下目录内的文件+子目录 fidone
[[email protected] ~]# !vimvim for02.sh#!/bin/bashcd /etc/for a in `ls /etc/`do [ -d $a ] && ls $a # 判断是否是目录,并列出其下文件和子目录 if [ -d $a ] then echo $a ls $a fidone~
Special for loop Example: When a list loop, a space or carriage return is used as a delimiter
[[email protected] ~]# mkdir xavi[[email protected] ~]# cd xavi/[[email protected] xavi]# ls[[email protected] xavi]# touch 1 2[[email protected] xavi]# touch 3\ 4.txt[[email protected] xavi]# ls1 2 3 4.txt[[email protected] xavi]# for i in `ls ./`; do echo $i ; done1234.txt //把3 4.txt一个文件拆分成两个了
Second, while loop
Grammar:
while test commanddo other commands done
Case 1: Write a script to monitor the load on the system
#!/bin/bashwhile : //:为死循环的意思,也相当于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]fisleep 30// 负载有时候不可能一秒钟就起来了,所以每隔30秒判断一次即可。我们使用sleep 去暂停30秒钟。然后再次执行。。。done
Case 2: Get a value and judge by interacting with the user.
The digital output of the user input.
- [] First of all, if the user does not input what to do?
- [] What happens if the user does not enter a pure number?
- [] Based on the number entered by the user, and output.
#!/bin/bashwhile :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 breakdoneecho $n
[] Continue: When the user does not enter the time, the first prompt, and then continue to let the user input. Until the input is a real number, jump out of the IF statement.
- [] Break: If it is a true number, jump out of the entire while statement.
Third, the use of break
Example:
#! /bin/bashfor i in `seq 1 5`do echo $iif [ $i == 3 ]then breakfiecho $idoneecho aaa
The following steps are performed:
[[email protected] xavi]# sh -x break.sh++ seq 1 5+ for i in ‘`seq 1 5`‘+ echo 11+ ‘[‘ 1 == 3 ‘]‘+ echo 11+ for i in ‘`seq 1 5`‘+ echo 22+ ‘[‘ 2 == 3 ‘]‘+ echo 22+ for i in ‘`seq 1 5`‘+ echo 33+ ‘[‘ 3 == 3 ‘]‘+ break+ echo aaaaaaaa
In other words, once a break is encountered, it jumps out of the current loop and makes the next step.
Iv. usage of continue:
Ignore the code under continue, and proceed to the next loop directly.
#! /bin/bashfor i in `seq 1 5`do echo $iif [ $i == 3 ]then continuefiecho $idoneecho aaa
The results of the implementation are as follows:
112234455aaa
That is, when executed to continue, any of the following statements are ignored directly: The next for loop is made directly.
V. Exit exits the entire script
#! /bin/bashfor i in `seq 1 5`do echo $iif [ $i == 3 ]then exitfiecho $idoneecho aaa
The following steps are performed:
++ seq 1 5+ for i in ‘`seq 1 5`‘+ echo 11+ ‘[‘ 1 == 3 ‘]‘+ echo 11+ for i in ‘`seq 1 5`‘+ echo 22+ ‘[‘ 2 == 3 ‘]‘+ echo 22+ for i in ‘`seq 1 5`‘+ echo 33+ ‘[‘ 3 == 3 ‘]‘+ exit
- [] When the line is straight to 3, exit the script directly.
Shell Programming (iii)-for loop, while loop, Break,continue,exit