Shell脚本(for循环,while循环,break跳出循环,continue结束本次循环)

for循环

语法:for 变量名 in 条件 ; do done;

案例一:

计算1-100所有数字的和。

脚本:

#!/bin/bash

sum=0

for i in `seq 1 100`

do

sum=$[$sum+$i]

done

echo $sum

结果:

[[email protected] ~]# sh 1-100.sh

5050

案例二:

列出/etc/sysconfig下所有子目录,并且使用ls -d命令查看。

脚本:

#/bin/bash

cd /etc/sysconfig

for i in `ls /etc/sysconfig`

do

if [ -d $i ]

then

ls -d $i

fi

done

结果:

[[email protected] ~]# sh syscon.sh

cbq

console

modules

network-scripts

for循环有一个值得注意的地方:

案例3:

我们创建几个文件,用for循环来ls他们。

[[email protected] shili]# ll

总用量 0

-rw-r--r-- 1 root root 0 1月  16 21:16 1.txt

-rw-r--r-- 1 root root 0 1月  16 21:16 2.txt

-rw-r--r-- 1 root root 0 1月  16 21:17 3 4.txt

[[email protected] shili]# for i in `ls ./`;do echo $i ;done

1.txt

2.txt

3

4.txt

所以写脚本的时候要注意

while循环

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

案例1:写一个脚本来监控系统负载,当系统负载大于10时,发邮箱警告。

脚本:

#/bin/bash

while :

do

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

if [ $load -gt 10 ]

then

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

fi

sleep 30

done

运行结果:

[[email protected] shell]# sh -x while.sh

+ :

++ w

++ head -1

++ awk -F 'load average: ' '{print $2}'

++ cut -d. -f1

+ load=0

+ '[' 0 -gt 10 ']'

+ sleep 30

案例二:

类似于之前写过的for循环的脚本,输入一个数字,如果不是数字返回一个字符串,如果输入为空返回一个字符串,如果是数字返回。

在看脚本之前,我们需要知道continue和break的意思。

continue是继续的意思,也就是当运行结果不满足条件时,在从头循环一遍。

break是跳出循环的意思。

脚本:

#/bin/bash

while :

do

read -p "please input a number: " n

if [ -z "$n" ]

then

echo "你需要输入东西."

continue

fi

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

if [ ! -z "$n1" ]

then

echo "你只能输入一个纯数字."

continue

fi

break

done

echo $n

运行结果:

[[email protected] shell]# sh while2.sh

please input a number: 1d

你只能输入一个纯数字.

please input a number:

你需要输入东西.

please input a number: 1

1

break

break在while循环中,我们提到了,这里来写一个脚本,加深印象

如果输入的数字小于等于3,则返回数字,如果输入的数字大于3,则返回aaaa

脚本:

#/bin/bash

read -p "please input a number: " i

if [ $i -lt 3 ]

then

echo $i

break

else

echo aaaa

fi

运行结果:

[[email protected] shell]# sh break.sh

please input a number: 1

1

[[email protected] shell]# sh break.sh

please input a number: 5

aaaa

continue结束本次循环,而break是跳出循环,要分清楚

[[email protected] shell]# cat continue.sh

#/bin/bash

for i in `seq 1 5`

do

echo $i

if [ $i -eq 3 ]

then

continue

fi

echo $i

done

[[email protected] shell]# sh continue.sh

1

1

2

2

3

4

4

5

5

[[email protected] shell]# cat break.sh

#/bin/bash

for i in `seq 1 5`

do

echo $i

if [ $i == 3 ]

then

break

fi

echo $i

done

[[email protected] shell]# sh break.sh

1

1

2

2

3

对比两个脚本我们可以发现,break相当于跳出循环,结束。而continue相当于结束本次循环,开始新的循环,

原文地址:http://blog.51cto.com/13407306/2070389

时间: 2024-08-24 22:33:57

Shell脚本(for循环,while循环,break跳出循环,continue结束本次循环)的相关文章

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

20.10 for循环1到100相加的值执行结果 打印出1-100的数字 if [ -d $a ]thenls -d $afi它相当于 [ -d $a ] && ls $a 上图创建的3空格4.txt是一个文件,查在for语句循环的时候将它拆分成3与4.txt两个文件了,说明for循环是以空格作为分隔符的,这个问题要注意20.11 20.12 while循环需求每隔半分钟检查一下系统的负载,当负载大于10的时候就发一封邮件,隔30 秒执行一次这是一个死循环脚本30秒执行一次这个脚本是让用户

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

一.for循环需求:计算1-100所有数字的和[[email protected] shell]# vi for1.sh#!/bin/bashfor i in seq 1 100doecho $i //先把100个数字打印出来done[[email protected] shell]# sh for1.sh //执行脚本打印出100个数字 继续修改脚本:[[email protected] shell]# vi for1.sh#!/bin/bashsum=0for i in seq 1 100d

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

原文地址:https://www.cnblogs.com/xiaobo-Linux/p/8920368.html

for与while循环、break跳出循环、continue结束本次循环、exit退出脚本

for循环 while循环 break跳出循环 continue结束本次循环 exit退出脚本 原文地址:http://blog.51cto.com/13515599/2106958

shell脚本 for循环、break跳出循环、continue结束本次循环

20.10 for循环 语法:for 变量名 in 条件; do ...; done ;案例1 [[email protected] shell]# cat for.sh #!/bin/bash sum=0 for i in `seq 1 100` do sum=$[$sum+$i] done echo $sum #输出的结果 [[email protected] shell]# sh for.sh 5050 文件列表循环 [[email protected] shell]# cat for2.

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

for 循环 当变量值在列表里,for循环即执行一次所有命令,使用变量名获取列表中的当前取值.命令可为任何有效的shell命令和语句. 语法:for 变量名 in 条件: do...;done 案例1 #!/bin/bashsum=0 // 给变量sum赋值for i in seq 1 100 // 给i赋值,从1到100dosum=$[ $sum + $i ]// 累加doneecho $sum// 输出结果为1到100的和 案例2 #!/bin/bashcd /etc/ // 切换到/etc

for循环,while循环,break跳出循环,continue结束本次循环,exit直接退出脚本

for循环 语法:for 变量名 in 条件; do -; done 案列1,算出1到10的数字相加等于多少并打印出过程 [[email protected] shell]# cat for1.sh #!/bin/bash sum=0 for i in `seq 1 10` do sum=$[$sum+$i] echo "$sum + $i" done echo $sum [[email protected] shell]# sh -x for1.sh + sum=0 ++ seq 1

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

for循环 ?语法:for 变量名 in 条件; do -; done 示例计算1到100所有数字的和 #!/bin/bash sum=0 for i in `seq 1 100` do sum=$[$sum+$i] done echo $sum 列出etc目录下的所有目录 #!/bin/bash cd /etc/ for a in `ls /etc/` do if [ -d $a ] then echo $a ls $a fi done while循环 语法 while 条件; do - ;

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

for循环 案例1:写个for循环的脚本需求,我让你计算1到100所有数字的和. 上图有了数字之后就去做加减法 [[email protected] shell]# sh -x for1.sh ##执行查询过程 + sum=0 ##sum=0 ++ seq 1 100 + for i in '`seq 1 100`' ##循环一次是1+0等于1 + sum=1 + for i in '`seq 1 100`' ##第二次循环是1+2等于3 + sum=3 + for i in '`seq 1 1