[root@akuilinux01 shell]# cat for1.sh
#!/bin/bash
sum=0
for i in `seq 1 10`
do
sum=$[$sum+$i]
echo "$sum + $i"
done
echo $sum
[root@akuilinux01 shell]# sh -x for1.sh
+ sum=0
++ seq 1 10
+ for i in ‘`seq 1 10`‘
+ sum=1
+ echo ‘1 + 1‘
1 + 1
+ for i in ‘`seq 1 10`‘
+ sum=3
+ echo ‘3 + 2‘
3 + 2
+ for i in ‘`seq 1 10`‘
+ sum=6
+ echo ‘6 + 3‘
6 + 3
+ for i in ‘`seq 1 10`‘
+ sum=10
+ echo ‘10 + 4‘
10 + 4
+ for i in ‘`seq 1 10`‘
+ sum=15
+ echo ‘15 + 5‘
15 + 5
+ for i in ‘`seq 1 10`‘
+ sum=21
+ echo ‘21 + 6‘
21 + 6
+ for i in ‘`seq 1 10`‘
+ sum=28
+ echo ‘28 + 7‘
28 + 7
+ for i in ‘`seq 1 10`‘
+ sum=36
+ echo ‘36 + 8‘
36 + 8
+ for i in ‘`seq 1 10`‘
+ sum=45
+ echo ‘45 + 9‘
45 + 9
+ for i in ‘`seq 1 10`‘
+ sum=55
+ echo ‘55 + 10‘
55 + 10
+ echo 55
55
[root@akuilinux01 shell]# cat for2.sh
#!/bin/bash
cd /root/shell/
for a in `ls /root/shell`
do
if [ -d $a ]
then
ls $a
fi
done
[root@akuilinux01 shell]# sh -x for2.sh
+ cd /root/shell/
++ ls /root/shell
+ for a in ‘`ls /root/shell`‘
+ ‘[‘ -d 1 ‘]‘
+ ls 1
1.txt 2.txt 3.txt
+ for a in ‘`ls /root/shell`‘
+ ‘[‘ -d 1.sh ‘]‘
+ for a in ‘`ls /root/shell`‘
+ ‘[‘ -d 2 ‘]‘
+ ls 2
a.txt b.txt c.txt
+ for a in ‘`ls /root/shell`‘
+ ‘[‘ -d 3 ‘]‘
+ ls 3
+ for a in ‘`ls /root/shell`‘
+ ‘[‘ -d 4 ‘]‘
+ ls 4
+ for a in ‘`ls /root/shell`‘
+ ‘[‘ -d case.sh ‘]‘
+ for a in ‘`ls /root/shell`‘
+ ‘[‘ -d file1.sh ‘]‘
+ for a in ‘`ls /root/shell`‘
+ ‘[‘ -d file2.sh ‘]‘
+ for a in ‘`ls /root/shell`‘
+ ‘[‘ -d for1.sh ‘]‘
+ for a in ‘`ls /root/shell`‘
+ ‘[‘ -d for2.sh ‘]‘
+ for a in ‘`ls /root/shell`‘
+ ‘[‘ -d if1.sh ‘]‘
+ for a in ‘`ls /root/shell`‘
+ ‘[‘ -d if2.sh ‘]‘
+ for a in ‘`ls /root/shell`‘
+ ‘[‘ -d if3.sh ‘]‘
+ for a in ‘`ls /root/shell`‘
+ ‘[‘ -d if4.sh ‘]‘
[root@akuilinux01 shell]# sh for2.sh
1.txt 2.txt 3.txt
a.txt b.txt c.txt
[root@akuilinux01 shell]# cat while1.sh
#!/bin/bash
#死循环,:是一个死循环,也可以写成1或者true
while :
do
#截取负载的值并赋给变量load
load=`w|head -1|awk -F ‘load average: ‘ ‘{print $2}‘|cut -d. -f1`
#如果负载大于10就报警并发送邮件,设置30秒查一次
if [ $load -gt 10 ]
then
/usr/lib/zabbix/alertscripts/mail.py xxxxx@qq.com "load is high:$load"
fi
sleep 30
done
[root@akuilinux01 shell]# sh -x while1.sh
+ :
++ head -1
++ cut -d. -f1
++ w
++ awk -F ‘load average: ‘ ‘{print $2}‘
+ load=0
+ ‘[‘ 0 -gt 10 ‘]‘
+ sleep 30
^C
[root@akuilinux01 shell]# uptime|awk -F ‘load average: ‘ ‘{print $2}‘
0.01, 0.03, 0.05
[root@akuilinux01 shell]# uptime|awk -F ‘load average: ‘ ‘{print $2}‘|cut -d. -f1
0
#w|head -1也可以写成uptime
[root@akuilinux01 shell]# w |head -1
01:02:36 up 58 min, 1 user, load average: 0.07, 0.04, 0.05
[root@akuilinux01 shell]# uptime
01:02:46 up 59 min, 1 user, load average: 0.06, 0.04, 0.05
[root@akuilinux01 shell]# cat while2.sh
#!/bin/bash
while :
do
read -p "Please input a number: " n
#如果你什么也没输入
if [ -z "$n" ]
then
echo "你需要输入一个数字"
#从头开始循环
continue
fi
#过滤数字并赋予n1
n1=`echo $n|sed ‘s/[0-9]//g‘`
#如果n1不为空,提示
if [ -n "$n1" ]
then
echo "你只能输入一个纯数字"
continue
fi
#退出循环
break
done
echo $n
[root@akuilinux01 shell]# sh while2.sh
Please input a number:
你需要输入一个数字
Please input a number: a
你只能输入一个纯数字
Please input a number: 2
2
[root@akuilinux01 shell]# cat break.sh
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i -eq 3 ]
then
break
fi
echo $i
done
echo aaaa
[root@akuilinux01 shell]# sh -x break.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 aaaa
aaaa
[root@akuilinux01 shell]# cat continue.sh
#!/bin/bash
for i in `seq 1 5`
do
if [ $i -eq 3 ]
then
continue
fi
echo $i
done
echo aaaa
[root@akuilinux01 shell]# sh -x continue.sh
++ seq 1 5
+ for i in ‘`seq 1 5`‘
+ ‘[‘ 1 -eq 3 ‘]‘
+ echo 1
1
+ for i in ‘`seq 1 5`‘
+ ‘[‘ 2 -eq 3 ‘]‘
+ echo 2
2
+ for i in ‘`seq 1 5`‘
+ ‘[‘ 3 -eq 3 ‘]‘
+ continue
+ for i in ‘`seq 1 5`‘
+ ‘[‘ 4 -eq 3 ‘]‘
+ echo 4
4
+ for i in ‘`seq 1 5`‘
+ ‘[‘ 5 -eq 3 ‘]‘
+ echo 5
5
+ echo aaaa
aaaa
[root@akuilinux01 shell]# cat exit.sh
#!/bin/bash
for i in `seq 1 5`
do
if [ $i -eq 3 ]
then
exit
fi
echo $i
done
echo aaaa
[root@akuilinux01 shell]# sh -x exit.sh
++ seq 1 5
+ for i in ‘`seq 1 5`‘
+ ‘[‘ 1 -eq 3 ‘]‘
+ echo 1
1
+ for i in ‘`seq 1 5`‘
+ ‘[‘ 2 -eq 3 ‘]‘
+ echo 2
2
+ for i in ‘`seq 1 5`‘
+ ‘[‘ 3 -eq 3 ‘]‘
+ exit
[root@akuilinux01 shell]# sh exit.sh
1
2
for循环,while循环,break跳出循环,continue结束本次循环,exit直接退出脚本
原文:http://blog.51cto.com/akui2521/2141965