码迷,mamicode.com
首页 > 系统相关 > 详细

2018-05-31 Linux学习

时间:2018-05-31 21:06:59      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:Linux学习

20.10 for循环

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

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

案例2
文件列表循环
#!/bin/bash
cd /etc/
for a in ls /etc/
do
if [ -d $a ]
then
ls $a
fi
done

操作过程

案例1

[root@linux-01 shell]# vim for1.sh
#!/bin/bash
sum=0
for i in seq 1 100
do
sum=$[$sum+$i]
done
echo $sum

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

案例2

[root@linux-01 shell]# vim for2.sh
#!/bin/bash
cd /etc/
for a in ls /etc/
do
[ -d $a ] && ls $a
done

20.11 while循环

语法 while 条件; do … ; done
案例1
#!/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" asldkfls@11.com
fi
sleep 30
done

案例2
#!/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" ]
then
echo "you just only input numbers."
continue
fi
break
done
echo $n

操作过程

案例1

[root@linux-01 shell]# uptime|awk -F ‘load average:‘ ‘{print $2}‘
 0.00, 0.01, 0.05
[root@linux-01 shell]# uptime|awk -F ‘load average:‘ ‘{print $2}‘|cut -d . -f1
 0
[root@linux-01 shell]# uptime|awk -F ‘load average:‘ ‘{print $2}‘|cut -d . -f1|sed ‘s/ //‘
0

[root@linux-01 shell]# vim while1.sh
#!/bin/bash
while true
do
load= w|head -1|awk -F ‘load average: ‘ ‘{print $2}‘|cut -d . -f1
if [ $load -gt 10 ]
then
/usr/local/sbin/mail.py xxx@qq.com "load high" "$load"
fi
sleep 30
done

案例2

[root@linux-01 shell]# vim while2.sh
#!/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" ]
then
echo "you just only input numbers."
continue
fi
break
done
echo $n

[root@linux-01 shell]# sh while2.sh 
Please input a number: sss
you just only input numbers.
Please input a number: ....
you just only input numbers.
Please input a number: 0
0

20.13 break跳出循环

#!/bin/bash
for i in seq 1 5
do
? ? echo $i
? ? if [ $i == 3 ]
? ? then
? ?? ???break
? ? fi
? ? echo $i
done
echo aaaaaaa

操作过程

[root@linux-01 shell]# 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 aaaaaa

[root@linux-01 shell]# sh break.sh 
1
1
2
2
3
aaaaaa

20.14 continue结束本次循环

忽略continue之下的代码,直接进行下一次循环
#!/bin/bash
for i in seq 1 5
do
? ? echo $i
? ? if [ $i == 3 ]
? ? then
? ?? ???continue
? ? fi
? ? echo $i
done
echo $i

操作过程

[root@linux-01 shell]# vim continue.sh

#!/bin/bash
for i in seq 1 5
do
echo $i
if [ $i -eq 3 ]
then
continue
fi
echo $i
done
echo aaaaaa

[root@linux-01 shell]# sh continue.sh 
1
1
2
2
3
4
4
5
5
aaaaaa

20.15 exit退出整个脚本

[root@linux-01 shell]# vim exit.sh

#!/bin/bash
for i in seq 1 5
do
echo $i
if [ $i -eq 3 ]
then
exit
fi
echo $i
done
echo aaaaaa

[root@linux-01 shell]# sh exit.sh 
1
1
2
2
3

2018-05-31 Linux学习

标签:Linux学习

原文地址:http://blog.51cto.com/9298822/2122593

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!