1.语法:for in do done
[root@weixing01 shell]# cat for.sh
#!/bin/bash
sum=0
for i in `seq 1 100`
do
sum=$[$sum+$i]
done
echo $sum
[root@weixing01 shell]# sh -x for.sh
+ sum=0
++ seq 1 100
+ for i in ‘`seq 1 100`‘
+ sum=1
+ for i in ‘`seq 1 100`‘
+ sum=3
+ for i in ‘`seq 1 100`‘
+ sum=6
+ for i in ‘`seq 1 100`‘
+ sum=10
+ for i in ‘`seq 1 100`‘
+ sum=15
+ for i in ‘`seq 1 100`‘
+ sum=21
+ for i in ‘`seq 1 100`‘
+ sum=28
+ for i in ‘`seq 1 100`‘
+ sum=36
+ for i in ‘`seq 1 100`‘
+ sum=45
+ for i in ‘`seq 1 100`‘
+ sum=55
+ for i in ‘`seq 1 100`‘
+ sum=66
+ for i in ‘`seq 1 100`‘
+ sum=78
+ for i in ‘`seq 1 100`‘
+ sum=91
+ for i in ‘`seq 1 100`‘
+ sum=105
+ for i in ‘`seq 1 100`‘
+ sum=120
+ for i in ‘`seq 1 100`‘
+ sum=136
+ for i in ‘`seq 1 100`‘
+ sum=153
+ for i in ‘`seq 1 100`‘
+ sum=171
+ for i in ‘`seq 1 100`‘
+ sum=190
+ for i in ‘`seq 1 100`‘
+ sum=210
+ for i in ‘`seq 1 100`‘
+ sum=231
+ for i in ‘`seq 1 100`‘
+ sum=253
+ for i in ‘`seq 1 100`‘
+ sum=276
+ for i in ‘`seq 1 100`‘
+ sum=300
+ for i in ‘`seq 1 100`‘
+ sum=325
+ for i in ‘`seq 1 100`‘
+ sum=351
+ for i in ‘`seq 1 100`‘
+ sum=378
+ for i in ‘`seq 1 100`‘
+ sum=406
+ for i in ‘`seq 1 100`‘
+ sum=435
+ for i in ‘`seq 1 100`‘
+ sum=465
+ for i in ‘`seq 1 100`‘
+ sum=496
+ for i in ‘`seq 1 100`‘
+ sum=528
+ for i in ‘`seq 1 100`‘
+ sum=561
+ for i in ‘`seq 1 100`‘
+ sum=595
+ for i in ‘`seq 1 100`‘
+ sum=630
+ for i in ‘`seq 1 100`‘
+ sum=666
+ for i in ‘`seq 1 100`‘
+ sum=703
+ for i in ‘`seq 1 100`‘
+ sum=741
+ for i in ‘`seq 1 100`‘
+ sum=780
+ for i in ‘`seq 1 100`‘
+ sum=820
+ for i in ‘`seq 1 100`‘
+ sum=861
+ for i in ‘`seq 1 100`‘
+ sum=903
+ for i in ‘`seq 1 100`‘
+ sum=946
+ for i in ‘`seq 1 100`‘
+ sum=990
+ for i in ‘`seq 1 100`‘
+ sum=1035
+ for i in ‘`seq 1 100`‘
+ sum=1081
+ for i in ‘`seq 1 100`‘
+ sum=1128
+ for i in ‘`seq 1 100`‘
+ sum=1176
+ for i in ‘`seq 1 100`‘
+ sum=1225
+ for i in ‘`seq 1 100`‘
+ sum=1275
+ for i in ‘`seq 1 100`‘
+ sum=1326
+ for i in ‘`seq 1 100`‘
+ sum=1378
+ for i in ‘`seq 1 100`‘
+ sum=1431
+ for i in ‘`seq 1 100`‘
+ sum=1485
+ for i in ‘`seq 1 100`‘
+ sum=1540
+ for i in ‘`seq 1 100`‘
+ sum=1596
+ for i in ‘`seq 1 100`‘
+ sum=1653
+ for i in ‘`seq 1 100`‘
+ sum=1711
+ for i in ‘`seq 1 100`‘
+ sum=1770
+ for i in ‘`seq 1 100`‘
+ sum=1830
+ for i in ‘`seq 1 100`‘
+ sum=1891
+ for i in ‘`seq 1 100`‘
+ sum=1953
+ for i in ‘`seq 1 100`‘
+ sum=2016
+ for i in ‘`seq 1 100`‘
+ sum=2080
+ for i in ‘`seq 1 100`‘
+ sum=2145
+ for i in ‘`seq 1 100`‘
+ sum=2211
+ for i in ‘`seq 1 100`‘
+ sum=2278
+ for i in ‘`seq 1 100`‘
+ sum=2346
+ for i in ‘`seq 1 100`‘
+ sum=2415
+ for i in ‘`seq 1 100`‘
+ sum=2485
+ for i in ‘`seq 1 100`‘
+ sum=2556
+ for i in ‘`seq 1 100`‘
+ sum=2628
+ for i in ‘`seq 1 100`‘
+ sum=2701
+ for i in ‘`seq 1 100`‘
+ sum=2775
+ for i in ‘`seq 1 100`‘
+ sum=2850
+ for i in ‘`seq 1 100`‘
+ sum=2926
+ for i in ‘`seq 1 100`‘
+ sum=3003
+ for i in ‘`seq 1 100`‘
+ sum=3081
+ for i in ‘`seq 1 100`‘
+ sum=3160
+ for i in ‘`seq 1 100`‘
+ sum=3240
+ for i in ‘`seq 1 100`‘
+ sum=3321
+ for i in ‘`seq 1 100`‘
+ sum=3403
+ for i in ‘`seq 1 100`‘
+ sum=3486
+ for i in ‘`seq 1 100`‘
+ sum=3570
+ for i in ‘`seq 1 100`‘
+ sum=3655
+ for i in ‘`seq 1 100`‘
+ sum=3741
+ for i in ‘`seq 1 100`‘
+ sum=3828
+ for i in ‘`seq 1 100`‘
+ sum=3916
+ for i in ‘`seq 1 100`‘
+ sum=4005
+ for i in ‘`seq 1 100`‘
+ sum=4095
+ for i in ‘`seq 1 100`‘
+ sum=4186
+ for i in ‘`seq 1 100`‘
+ sum=4278
+ for i in ‘`seq 1 100`‘
+ sum=4371
+ for i in ‘`seq 1 100`‘
+ sum=4465
+ for i in ‘`seq 1 100`‘
+ sum=4560
+ for i in ‘`seq 1 100`‘
+ sum=4656
+ for i in ‘`seq 1 100`‘
+ sum=4753
+ for i in ‘`seq 1 100`‘
+ sum=4851
+ for i in ‘`seq 1 100`‘
+ sum=4950
+ for i in ‘`seq 1 100`‘
+ sum=5050
+ echo 5050
5050
2.案例2:
[root@weixing01 shell]# cat for2.sh
#!/bin/bash
cd /etc/
for a in ls /etc/
do
if [ -d $a ]
then
ls $a
fi
done
[root@weixing01 shell]# sh for2.sh
adjtime group named.iscdlv.key rsyslog.d
aliases group- named.rfc1912.zones rwtab
aliases.db grub2.cfg named.root.key rwtab.d
alternatives grub.d netconfig samba
anacrontab gshadow NetworkManager sasl2
asound.conf gshadow- networks screenrc
audisp gss nfs.conf securetty
audit gssproxy nfsmount.conf security
bash_completion.d host.conf nsswitch.conf selinux
bashrc hostname nsswitch.conf.bak services
binfmt.d hosts odbcinst.ini sestatus.conf
centos-release hosts.allow openldap shadow
centos-release-upstream hosts.deny opt shadow-
3.注意for循环中以空格为分隔符:
[root@weixing01 weix]# touch 1 2
[root@weixing01 weix]# touch 3\ 4.txt
[root@weixing01 weix]# ls
1 2 3 4.txt
[root@weixing01 weix]# ls3
-bash: ls3: 未找到命令
[root@weixing01 weix]# ls 3
ls: 无法访问3: 没有那个文件或目录
[root@weixing01 weix]# ls -l
总用量 0
-rw-r--r-- 1 root root 0 4月 19 22:16 1
-rw-r--r-- 1 root root 0 4月 19 22:16 2
-rw-r--r-- 1 root root 0 4月 19 22:16 3 4.txt
[root@weixing01 weix]# for i in `ls ./`; do echo $i ; done
1
2
3
4.txt
1.案例1:
[root@weixing01 shell]# cat whil2.sh
#!/bin/bash
while true
do
load=`w|head 1|awk -F ‘load average: ‘ ‘{print $2}‘ |cut -d. -f1`
if [ $load -gt 10 ]
then
echo "bbb"
fi
sleep 30
done
[root@weixing01 shell]# sh -x whil2.sh
+ true
++ w
++ head 1
head: 无法打开"1" 读取数据: 没有那个文件或目录
++ awk -F ‘load average: ‘ ‘{print $2}‘
++ cut -d. -f1
+ load=
+ ‘[‘ -gt 10 ‘]‘
whil2.sh: 第 5 行:[: -gt: 期待一元表达式
+ sleep 30
2.案例2
[root@weixing01 shell]# cat while3.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@weixing01 shell]# sh -x while3.sh
+ :
+ read -p ‘Please input a number: ‘ n
Please input a number: 123fjkagj
+ ‘[‘ -z 123fjkagj ‘]‘
++ echo 123fjkagj
++ sed ‘s/[0-9]//g‘
+ n1=fjkagj
+ ‘[‘ -n fjkagj ‘]‘
+ echo ‘you just only input numbers.‘
you just only input numbers.
+ continue
+ :
+ read -p ‘Please input a number: ‘ n
Please input a number: 123124
+ ‘[‘ -z 123124 ‘]‘
++ echo 123124
++ sed ‘s/[0-9]//g‘
+ n1=
+ ‘[‘ -n ‘‘ ‘]‘
+ break
+ echo 123124
123124
[root@weixing01 shell]# 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 aaaa
[root@weixing01 shell]# 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 aaaa
aaaa
[root@weixing01 shell]# cat continue1.sh
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i -eq 3 ]
then
continue
fi
echo $i
done
echo aaaa
[root@weixing01 shell]# sh -x continue1.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 aaaa
aaaa
[root@weixing01 shell]# cat exit1.sh
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i -eq 3 ]
then
exit
fi
echo $i
done
echo aaaa
[root@weixing01 shell]# sh -x exit1.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
原文:http://blog.51cto.com/13517254/2105573