1、数字循环

[root@zabbix ~]# for i in {1..10};do echo $(expr $i \* 3);done;
3
6
9
12
15
18
21
24
27
30

[root@zabbix ~]# for i in `seq 1 10`;do echo $(expr $i \* 3);done;
3
6
9
12
15
18
21
24
27
30

[root@zabbix ~]# for((i=1;i<=10;i++));do echo $(expr $i \* 3);done
3
6
9
12
15
18
21
24
27
30

[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 appoint
f2 is appoint
f3 is appoint

[root@zabbix ~]# list="rootfs usr data data2"
[root@zabbix ~]# for i in $list;do echo $i is appoint;done
rootfs is appoint
usr is appoint
data is appoint
data2 is appoint

3、路径查找

[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.sh
key.sh
li.sh
long.sh
test.sh

 

4、for循环嵌套if...then....else语句

[root@zabbix ~]# cat li.sh
#!/bin/bash
for i in {1..100}
do
if (( $i \* 5 == 100 ));then
echo $i
fi
done

###打印1到100内的数字,如果这个数字乘以5等于100,那么打印该数字

[root@zabbix ~]# cat li.sh
#!/bin/bash
for i in {1..10}
do
if (( $i/5 == 0 ));then
echo $i
fi
done

###打印1到10内的数字,如果这个数字除以5的余数等与0,那么打印出所有数字

 

posted on 2018-11-21 14:23  哪是什么大佬  阅读(362)  评论(0编辑  收藏  举报