iptables -A INPUT -p tcp --dport $x -m state --state NEW -j ACCEPT
done
-------for variable in values--------------字符串数组依次赋值
#!/bin/sh
for i in a b c 字符串列表A B C
字符串用空格分隔,没有括号,没有逗号, 然后循环将其依次赋给变量i
变量没有$
do
echo "i is $i"
done
[macg@machome ~]$ sh test.sh
i is a
i is b
i is c
-------for in 里,变量和*不等价-------
#!/bin/bash
for i in *.h ;
do
cat ${i}.h
done
[macg@vm test]$ ./tip.sh
cat: *.h.h: No such file or directory
$i代表的是整个路径,而不是*.h里的.h前面的部分
改正
#!/bin/bash
for i in *.h
do
cat $i
done
[macg@vm test]$ echo hahaha >>1.h
[macg@vm test]$ echo ha >>2.h
[macg@vm test]$ ./tip.sh
hahaha
ha
例2:
for i in /etc/profile.d/*.sh
do
$i
done
$i代表的是/etc/profile.d/color.sh,
/etc/profile.d/alias.sh, /etc/profile.d/default.sh
-------for in 对(命令行,函数)参数遍历-------
test()
{
local i
for i in $* ; do
echo "i is $i"
done
}
$*是字符串:以"参数1 参数2 ... " 形式保存所有参数
$i是变量i的应用表示
[macg@machome ~]$ sh test.sh p1 p2 p3 p4
i is p1
i is p2
i is p3
i is p4
------- for in语句与通配符*合用,批量处理文件-------
批量改文件名
[root@vm testtip]# ls
aaa.txt ccc.txt eee.txt ggg.txt hhh.txt jjj.txt lll.txt nnn.txt
bbb.txt ddd.txt fff.txt go.sh iii.txt kkk.txt mmm.txt ooo.txt
[root@vm testtip]# cat go.sh