for 变量名 in 变量取值列表;do 指令… done 其中取值列表通常又有5种方式: (1)直接给出列表,列表中间用空格隔开 [root@centos7 ~]#for i in 1 3 5;do echo $i;done 1 3 5 (2)整数列表 (a) {start..end} [root@centos7 ~]#for i in {1..5..2};do echo $i;done 1 3 5 (b)$(seq start step end) [root@centos7 ~]#for i in $(seq 1 2 5);do echo $i;done 1 3 5 (3)返回命令列表 [root@centos7 ~]#mkdir test [root@centos7 ~]#cd test/ [root@centos7 ~/test]#touch 1.sh [root@centos7 ~/test]#touch 3.sh [root@centos7 ~/test]#touch 5.sh [root@centos7 ~/test]#for i in $(ls);do echo $i;done 1.sh 3.sh 5.sh (4)使用glob,如:*.sh [root@centos7 ~/test]#for i in *.sh; do echo $i;done #选择当前目录下满足*.sh 1.sh 3.sh 5.sh (5)使用变量引用如$@,$* [root@centos7 ~/test]#vim 1 [root@centos7 ~/test]#chmod +x 1 1 #!/bin/bash 2 for i in $@;do 3 rm -rf $i 4 done [root@centos7 ~/test]#./1 1.sh 3.sh 5.sh [root@centos7 ~/test]#ls 1 #验证1.sh 3.sh 5.sh已经删除 |