1、数字循环
[root@zabbix ~]# for i in {1..10};do echo $(expr $i \* 3);done;
36912151821242730[root@zabbix ~]# for i in `seq 1 10`;do echo $(expr $i \* 3);done;
36912151821242730[root@zabbix ~]# for((i=1;i<=10;i++));do echo $(expr $i \* 3);done
36912151821242730[root@zabbix ~]# awk 'BEGIN{for(i=1; i<=10; i++) print i*3}'
2、字符型循环
[root@zabbix ~]# for i in f1 f2 f3;do echo $i is appoint ;done
f1 is appointf2 is appointf3 is appoint[root@zabbix ~]# list="rootfs usr data data2"
[root@zabbix ~]# for i in $list;do echo $i is appoint;done rootfs is appointusr is appointdata is appointdata2 is appoint3、路径查找
[root@zabbix ~]# for file in /media/*;do echo $file \!;done
/media/CentOS_BuildTag !/media/EFI !/media/EULA !/media/GPL !/media/images !/media/isolinux !/media/LiveOS !/media/Packages !/media/repodata !/media/RPM-GPG-KEY-CentOS-7 !/media/RPM-GPG-KEY-CentOS-Testing-7 !/media/TRANS.TBL ![root@zabbix ~]# for file in $(ls *.sh); do echo $file;done
a.shkey.shli.shlong.shtest.sh
4、for循环嵌套if...then....else语句
[root@zabbix ~]# cat li.sh
#!/bin/bashfor i in {1..100}doif (( $i \* 5 == 100 ));then echo $ifidone###打印1到100内的数字,如果这个数字乘以5等于100,那么打印该数字
[root@zabbix ~]# cat li.sh
#!/bin/bashfor i in {1..10}doif (( $i/5 == 0 ));then echo $ifidone###打印1到10内的数字,如果这个数字除以5的余数等与0,那么打印出所有数字