for循环结构分类如下:
1.遍历式结构
语法如下:
for variable in list
do
Statement
done
注意:在此结构中变量取值列表 in list 可以省略,省略相当于in "$@" ,那么使用for i就等价于for i in "$@"
shell实例0:
[root@ChangerLee shell_note]# cat for_no_list.sh
#!/bin/bash
for i
do
echo $i
done
[root@ChangerLee shell_note]# sh for_no_list.sh apache puppet zabbix
apache
puppet
zabbix
(1)用seq产生一个序列范围,来控制遍历的次数
Shell实例1:
[root@ChangerLee 循环控制结构]# cat for_struct.sh
#!/bin/bash
#an instantiation of for echo
for i in `seq 10`
do
echo $i
done
[root@ChangerLee 循环控制结构]# sh for_struct.sh
1
2
3
4
5
6
7
8
9
10
Shell实例2:
[root@ChangerLee 循环控制结构]# cat for_struct_v1.sh#!/bin/bash
#an instantiation of for echo
for i in {a..j}
do
echo $i
done
[root@ChangerLee 循环控制结构]# sh for_struct_v1.sh
a
b
c
d
e
f
g
h
i
j
Shell实例3:
[root@ChangerLee 循环控制结构]# cat for_struct_v2.sh
#!/bin/bash
#an instantiation of for echo
for i in $(seq 10)
do
echo $i
done
[root@ChangerLee 循环控制结构]# sh for_struct_v2.sh
1
2
3
4
5
6
7
8
9
10
Shell实例4:
[root@ChangerLee 循环控制结构]# cat for_struct_v3.sh
#!/bin/bash
#an instantiation of for echo
for i in $(seq 1 2 10)
do
echo $i
done
[root@ChangerLee 循环控制结构]# sh for_struct_v3.sh
1
3
5
7
9
Shell实例5:
[root@ChangerLee 循环控制结构]# cat for_struct_v4.sh
#!/bin/bash
#an instantiation of for echo
for i in `seq 10`
do
touch file_$i
chmod 755 file_$i
done
[root@ChangerLee 循环控制结构]# sh for_struct_v4.sh
[root@ChangerLee 循环控制结构]# ls file*
file_1 file_10 file_2 file_3 file_4 file_5 file_6 file_7 file_8 file_9
shell实例6:
[root@ChangerLee 循环控制结构]# cat fomvname.sh
#!/bin/bash
#multi-change name
for i in `ls file_*`
do
mv $i `echo $i |cut -d _ -f2`.conf
done
[root@ChangerLee 循环控制结构]# sh fomvname.sh
[root@ChangerLee 循环控制结构]# ls *.conf
10.conf 1.conf 2.conf 3.conf 4.conf 5.conf 6.conf 7.conf 8.conf 9.conf
(2)命令不是连续情况:
shell实例7:
[root@ChangerLee 循环控制结构]# cat for_struct_v5.sh
#!/bin/bash
#an instantiation of for echo
for i in file_log file_bin file_config
do
mkdir $i
chmod 755 $i
echo $i
done
[root@ChangerLee 循环控制结构]# sh for_struct_v5.sh
file_log
file_bin
file_config
[root@ChangerLee 循环控制结构]# ll |grep ^d
drwxr-xr-x. 2 root root 4096 Aug 9 23:01 file_bin
drwxr-xr-x. 2 root root 4096 Aug 9 23:01 file_config
drwxr-xr-x. 2 root root 4096 Aug 9 23:01 file_log
shell实例8:
drwxrwxrwx. 2 root root 4096 Aug 9 23:01 file_bin
drwxrwxrwx. 2 root root 4096 Aug 9 23:01 file_config
drwxrwxrwx. 2 root root 4096 Aug 9 23:01 file_log
[root@ChangerLee 循环控制结构]# cat for_list_dir.sh
#!/bin/bash
#列出当前目录的下目录文件
for num in `ls -F|grep /`
do
echo $num
done
[root@ChangerLee 循环控制结构]# sh for_list_dir.sh
file_bin/
file_config/
file_log/
2.C语言风格的结构
当for循环的条件为真时,执行循环体的语句,语法如下:
for((expr1;expr2;expr3))
do
Statement
done
shell实例9:
创建一年中[root@ChangerLee 循环控制结构]# cat for_struct_v6.sh
#!/bin/bash
for ((month=1;${month}<=12;month++))
do
echo "create database analyse_$month ..."
sleep 1
mysql -e "create database analyse_$month"
done
一月至十二月所使用的数据库,将调用统计的结果插入到相应的月份中
[root@ChangerLee 循环控制结构]# sh for_struct_v6.sh
create database analyse_1 ...
create database analyse_2 ...
create database analyse_3 ...
create database analyse_4 ...
create database analyse_5 ...
create database analyse_6 ...
create database analyse_7 ...
create database analyse_8 ...
create database analyse_9 ...
create database analyse_10 ...
create database analyse_11 ...
create database analyse_12 ...
[root@ChangerLee 循环控制结构]# mysql -e "show databases"
+--------------------+
| Database |
+--------------------+
| information_schema |
| analyse_1 |
| analyse_10 |
| analyse_11 |
| analyse_12 |
| analyse_2 |
| analyse_3 |
| analyse_4 |
| analyse_5 |
| analyse_6 |
| analyse_7 |
| analyse_8 |
| analyse_9 |
| mysql |
| performance_schema |
| test |
+--------------------+