天天看点

for循环 while循环 break跳出循环 continue结束本次循环 exit退出整

一、for循环

for循环 while循环 break跳出循环 continue结束本次循环 exit退出整

需求:计算1-100所有数字的和

[root@linux-01 shell]# vi for1.sh

#!/bin/bash

for i in

seq 1 100

do

echo $i //先把100个数字打印出来

done

[root@linux-01 shell]# sh for1.sh //执行脚本打印出100个数字

继续修改脚本:

sum=0

seq 1 100

sum=$[$sum+$i]

echo $sum

[root@linux-01 shell]# sh -x for1.sh //执行脚本,查看执行过程

继续完善脚本:

seq 1 100

echo "$sum + $i"

[root@linux-01 shell]# sh for1.sh

for循环 while循环 break跳出循环 continue结束本次循环 exit退出整

[root@linux-01 shell]# vi for2.sh

cd /etc/

for a in i

ls /etc/

if [ -d $a ]

then

echo $a

ls $a

fi

[root@linux-01 shell]# sh -x for2.sh //执行脚本查看详细执行过程

[root@linux-01 shell]# mkdir aming //创建一个aming目录

[root@linux-01 shell]# cd aming/

[root@linux-01 aming]# ls

[root@linux-01 aming]# touch 1 2 //创建1和2文件

[root@linux-01 aming]# touch 3\ 4.txt //创建一个3脱义 4.txt文件

[root@linux-01 aming]# ls -l

total 0

-rw-r--r-- 1 root root 0 Jul 15 20:40 1

-rw-r--r-- 1 root root 0 Jul 15 20:40 2

-rw-r--r-- 1 root root 0 Jul 15 20:41 3 4.txt //可以看到这是一个文件

[root@linux-01 aming]# for i in

ls ./

; do echo $i;done

1

2

3

4.txt

//for循环使用这种命令的形式的时候,空格或者回车都作为一个分隔符

二、while循环

for循环 while循环 break跳出循环 continue结束本次循环 exit退出整

语法 while 条件;do...;done

案例1:

需求:每隔半分钟检查下系统负载,当系统负载大于10的时候就发一封邮件

[root@linux-01 aming]# vim while1.sh

while true //ture和冒号都是真的意思,即死循环

load=

w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1

if [ $load -gt 10 ]

/usr/local/sbin/mail.py [email protected] "load high" "$load"

sleep 30

[root@linux-01 aming]# sh -x while1.sh

为了不让脚本在执行过程中终止,可以进入一个screen,把进程丢到后台去运行

screen命令详细使用方法参考网址:http://man.linuxde.net/screen

我们可以把脚本中的内容拆分开执行下

[root@linux-01 aming]# w //使用w命令可以查看系统负载

21:03:12 up 22:27, 1 user, load average: 0.00, 0.01, 0.05

USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT

root pts/1 192.168.238.1 11:43 0.00s 0.49s 0.01s w

[root@linux-01 aming]# uptime

21:03:16 up 22:27, 1 user, load average: 0.00, 0.01, 0.05

[root@linux-01 aming]# uptime|awk -F 'load average: ' '{print $2}'

0.00, 0.01, 0.05

[root@linux-01 aming]# uptime|awk -F 'load average:' '{print $2}'|cut -d . -f1 //看到0前面有空格

让0前面没有空格的两个方法:

方法一:

[root@linux-01 aming]# uptime|awk -F 'load average: ' '{print $2}'|cut -d . -f1 //‘’前面加个空格

方法二:

[root@linux-01 aming]# uptime|awk -F 'load average:' '{print $2}'|cut -d . -f1|sed 's/ //' //加sed

while循环案例2

for循环 while循环 break跳出循环 continue结束本次循环 exit退出整

死循环需求:

在循环过程当中,需要人为的交互,输入一个数字,可定要检测数字是不是自己想要的东西

[root@linux-01 aming]# vim while2.sh

while :

read -p "Please input a number: " n

if [ -z "$n" ]

echo "you need input sth."

continue //continue表示继续执行上面的do,不执行下面的内容

fi //如果输入的有东西,就不执行上面两行命令,直接执行下面的命令

n1=

echo $n|sed 's/[0-9]//g'

if [ -n "$n1" ] //-n等同于! -z

echo "you just only input numbers."

continue //继续上面的循环

break //break就是退出这个脚本的循环,退出来之后直接echo $n

echo $n

  • for i in '

    seq 1 5

    '
  • echo 1
  • '[' 1 -eq 3 ']'
  • seq 1 5

  • echo 2
  • '[' 2 -eq 3 ']'
  • seq 1 5

  • echo 3
  • '[' 3 -eq 3 ']'
  • break
  • echo aaaaaaa

    aaaaaaa

    [root@linux-01 aming]# sh break.sh

  • seq 1 5

  • seq 1 5

  • seq 1 5

  • continue
  • seq 1 5

  • echo 4

    4

  • '[' 4 -eq 3 ']'
  • seq 1 5

  • echo 5

    5

  • '[' 5 -eq 3 ']'