标签:stat sha while循环 min sleep 退出 tps pki ++
for循环#打印1到100
[root@garytao-01 shell]# vi for1.sh
[root@garytao-01 shell]# cat for1.sh
#!/bin/bash
for i in `seq 1 100 `
do
echo $i
done
[root@garytao-01 shell]# sh for1.sh
1
2
3
4
5
6
7
8
9
10
11
...
#打印数字叠加的值
[root@garytao-01 shell]# vi for1.sh
[root@garytao-01 shell]# cat for1.sh
#!/bin/bash
sum=0
for i in `seq 1 100 `
do
sum=$[$sum+$i] #核心语句
done
echo $sum
[root@garytao-01 shell]# sh for1.sh
5050
#每次循环相加的值
[root@garytao-01 shell]# vi for1.sh
[root@garytao-01 shell]# cat for1.sh
#!/bin/bash
sum=0
for i in `seq 1 100`
do
echo "$sum + $i"
sum=$[$sum+$i]
echo $sum
done
echo $sum
[root@garytao-01 shell]# sh for1.sh
0 + 1
1
1 + 2
3
3 + 3
6
....
4950 + 100
5050
[root@garytao-01 shell]# vi for2.sh
[root@garytao-01 shell]# cat for2.sh
#!/bin/bash
cd /etc/
for a in ls /etc/
do
if [ -d $a ]
then
echo $a
ls $a
fi
done
[root@garytao-01 shell]# sh for2.sh
/etc/
adjtime exports.d ld.so.conf.d pki shadow-
aliases favicon.png lftp.conf plymouth shells
aliases.db filesystems libaudit.conf pm skel
alternatives firewalld libnl polkit-1 ssh
anacrontab fonts libuser.conf popt.d ssl
asound.conf fstab locale.conf postfix statetab
audisp fuse.conf localtime ppp statetab.d
audit gcrypt login.defs prelink.conf.d subgid
bash_completion.d GeoIP.conf logrotate.conf printcap subuid
需求:每隔半分钟检查系统负载,当系统负载大于10的时候就发一封邮件告警。
脚本示例:
[root@garytao-01 aming]# vi while1.sh
[root@garytao-01 aming]# cat 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
[root@garytao-01 aming]# sh -x while1.sh
+ true
++ w
++ head -1
++ awk -F ‘load average: ‘ ‘{print $2}‘
++ cut -d. -f1
+ load=0
+ ‘[‘ 0 -gt 10 ‘]‘
+ sleep 30
+ true
++ w
++ head -1
++ awk -F ‘load average: ‘ ‘{print $2}‘
++ cut -d. -f1
+ load=0
+ ‘[‘ 0 -gt 10 ‘]‘
+ sleep 30
^C
[root@garytao-01 aming]#
需求:在循环过程中需要人为的输入一个数字
脚本示例:
[root@garytao-01 shell]# vim while2.sh
[root@garytao-01 shell]# cat 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 [ ! -z "$n1" ]
then
echo "you just only input numbers."
continue
fi
break
done
echo $n
[root@garytao-01 shell]# sh -x while2.sh
+ :
+ read -p ‘Please input a number: ‘ n
Please input a number:
+ ‘[‘ -z ‘‘ ‘]‘
+ echo ‘you need input sth.‘
you need input sth.
+ continue
+ :
+ read -p ‘Please input a number: ‘ n
Please input a number: 23
+ ‘[‘ -z 23 ‘]‘
++ echo 23
++ sed ‘s/[0-9]//g‘
+ n1=
+ ‘[‘ ‘!‘ -z ‘‘ ‘]‘
+ break
+ echo 23
23
[root@garytao-01 shell]# vim while2.sh
[root@garytao-01 shell]# cat 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@garytao-01 shell]# sh -x while2.sh
+ :
+ read -p ‘Please input a number: ‘ n
Please input a number:
+ ‘[‘ -z ‘‘ ‘]‘
+ echo ‘you need input sth.‘
you need input sth.
+ continue
+ :
+ read -p ‘Please input a number: ‘ n
Please input a number: 32kgg
+ ‘[‘ -z 32kgg ‘]‘
++ echo 32kgg
++ sed ‘s/[0-9]//g‘
+ n1=kgg
+ ‘[‘ -n kgg ‘]‘
+ echo ‘you just only input numbers.‘
you just only input numbers.
+ continue
+ :
+ read -p ‘Please input a number: ‘ n
Please input a number: 45
+ ‘[‘ -z 45 ‘]‘
++ echo 45
++ sed ‘s/[0-9]//g‘
+ n1=
+ ‘[‘ -n ‘‘ ‘]‘
+ break
+ echo 45
45
[root@garytao-01 aming]# vi break1.sh
[root@garytao-01 aming]# cat break1.sh
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i -eq 3 ]
then
break
fi
echo $i
done
echo aaaaa
[root@garytao-01 aming]# sh break1.sh
1
1
2
2
3
aaaaa
[root@garytao-01 aming]# sh -x break1.sh
++ seq 1 5
+ for i in ‘`seq 1 5`‘
+ echo 1
1
+ ‘[‘ 1 -eq 3 ‘]‘
+ echo 1
1
+ for i in ‘`seq 1 5`‘
+ echo 2
2
+ ‘[‘ 2 -eq 3 ‘]‘
+ echo 2
2
+ for i in ‘`seq 1 5`‘
+ echo 3
3
+ ‘[‘ 3 -eq 3 ‘]‘
+ break
+ echo aaaaa
aaaaa
[root@garytao-01 aming]#
[root@garytao-01 aming]# vi continue.sh
[root@garytao-01 aming]# cat continue.sh
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i -eq 3 ]
then
continue
fi
echo $i
done
echo aaaaa
[root@garytao-01 aming]# sh continue.sh
1
1
2
2
3
4
4
5
5
aaaaa
[root@garytao-01 aming]# sh -x continue.sh
++ seq 1 5
+ for i in ‘`seq 1 5`‘
+ echo 1
1
+ ‘[‘ 1 -eq 3 ‘]‘
+ echo 1
1
+ for i in ‘`seq 1 5`‘
+ echo 2
2
+ ‘[‘ 2 -eq 3 ‘]‘
+ echo 2
2
+ for i in ‘`seq 1 5`‘
+ echo 3
3
+ ‘[‘ 3 -eq 3 ‘]‘
+ continue
+ for i in ‘`seq 1 5`‘
+ echo 4
4
+ ‘[‘ 4 -eq 3 ‘]‘
+ echo 4
4
+ for i in ‘`seq 1 5`‘
+ echo 5
5
+ ‘[‘ 5 -eq 3 ‘]‘
+ echo 5
5
+ echo aaaaa
aaaaa
[root@garytao-01 aming]#
[root@garytao-01 aming]# vi exit.sh
[root@garytao-01 aming]# cat exit.sh
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i -eq 3 ]
then
exit
fi
echo $i
done
echo aaaaa
[root@garytao-01 aming]# sh exit.sh
1
1
2
2
3
[root@garytao-01 aming]# sh -x exit.sh
++ seq 1 5
+ for i in ‘`seq 1 5`‘
+ echo 1
1
+ ‘[‘ 1 -eq 3 ‘]‘
+ echo 1
1
+ for i in ‘`seq 1 5`‘
+ echo 2
2
+ ‘[‘ 2 -eq 3 ‘]‘
+ echo 2
2
+ for i in ‘`seq 1 5`‘
+ echo 3
3
+ ‘[‘ 3 -eq 3 ‘]‘
+ exit
[root@garytao-01 aming]#
for循环、while循环、break循环、continue结束本次循环、exit退出整个脚本
标签:stat sha while循环 min sleep 退出 tps pki ++
原文地址:http://blog.51cto.com/taoxie/2069945