for循环写法总结
关于shell中的for循环用法很多,感觉很全面,所以就转过来研究研究,嘿嘿...
1、 for((i=1;i<=10;i++))
[[email protected] ~]# for ((i=1;i<3;i++ )); do echo $(($i*4)); done
4
8
2、在shell中常用的是for i in $(seq 10)
[[email protected] ~]# for i in $(seq 2); do echo $(($i*4)); done
4
8
3、for i in {1..10}
[[email protected] ~]# for i in {1..2}; do echo $(($i*4)); done
4
8
4、for i in stringchar {1..10}
[[email protected] ~]# for i in user{1..2}; do echo $i; done
user1
user2
5、for i in `ls`
[[email protected] ~]# for i in `ls`; do echo $i; done
6、for i inf1 f2 f3 ;do
[[email protected] ~]# for i in 2 8 20; do echo $(($i*4)); done
8
32
80
7、for i in *.txt
[[email protected] ~]# for i in *.txt ;do echo $i ; done #相当于ls *.txt
second.txt
userinfo.txt
8、for i in $(ls *.txt)#利用` `或$( )的将多行合为一行,实际是合为一个字符串数组
[[email protected] ~]# for i in $(ls *.txt) ; do echo $i ; done
second.txt
userinfo.txt
9、for i in $* ; do # $*表示脚本输入的所有参数
10、for i in ${arr[@]}
11、for File in /proc/sys/net/ipv4/conf/*/accept_redirects; do
13、awk ‘BEGIN{for(i=1; i<=10; i++) print i}‘ #AWK中的for循环写法和C语言一样的
12、用for in语句自动对字符串按空格遍历的特性,对多个目录遍历
LIST="rootfs usr data data2"
for d in $LIST; do
---end---