shell笔记(二)——for循环
来源:互联网 发布:sql in 两个字段 编辑:程序博客网 时间:2024/05/01 08:23
先通过一个例子认识下shell的for循环:
#!/bin/bashecho "----start----"for((i=0;i<=100;i++))doif((i%3==0))thenecho $i >> date.txt;fidonecat date.txt |xargs -n 20执行结果:
kldong@ubuntu:~/learning/shell$ ./test_for.sh ----start----0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 5760 63 66 69 72 75 78 81 84 87 90 93 96 99
以上程序的目的简单,就是找出100以内可以被3整除的整数,并以每行20个打印出来。
for in 格式
for 无$变量 in 字符串
do
$变量
done一简单的字符串 枚举遍历法,利用for in格式对字符串按空格切份的功能
SERVICES="80 22 25 110 8000 23 20 21 3306 "
for x in $SERVICES
do
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
for i in *.txt *.txt相当于一个字符串数组,依次循环赋值给i
do
mv "$i" "$i.bak"
done[root@vm testtip]# sh go.sh
[root@vm testtip]# ls
aaa.txt.bak ccc.txt.bak eee.txt.bak ggg.txt.bak hhh.txt.bak jjj.txt.bak lll.txt.bak nnn.txt.bak bbb.txt.bak ddd.txt.bak fff.txt.bak go.sh iii.txt.bak kkk.txt.bak mmm.txt.bak ooo.txt.bak
for in语句与` `和$( )合用,利用` `或$( )的将多行合为一行的缺陷,实际是合为一个字符串数组
for i in $(ls *.txt)
do
echo $i
done[macg@machome ~]$ sh test
111-tmp.txt
111.txt
22.txt
33.txt 或者说,利用for in克服` `和$( ) 的多行合为一行的缺陷
利用for in 自动对字符串按空格遍历的特性,对多个目录遍历
LIST="rootfs usr data data2"
for d in $LIST; do
mount /backup/$d
rsync -ax --exclude fstab --delete /$d/ /backup/$d/
umount /backup/$d
done
for 无$变量 in 字符串
do
done一简单的字符串 枚举遍历法,利用for in格式对字符串按空格切份的功能
SERVICES="80
for
#!/bin/sh
for i in a b c
do
echo "i is $i"
done
i is a
i is b
i is c
#!/bin/bash
for i in *.h ;
do
cat ${i}.h
done
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 ha >>2.h
[macg@vm test]$ ./tip.sh
hahaha
ha
for i in /etc/profile.d/*.sh
done
/etc/profile.d/alias.sh, /etc/profile.d/default.sh
test()
{
}
$*是字符串:以"参数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
[root@vm testtip]# ls
aaa.txt
bbb.txt
for i in *.txt
do
mv "$i" "$i.bak"
done[root@vm testtip]# sh go.sh
[root@vm testtip]# ls
aaa.txt.bak
for i in $(ls *.txt)
do
echo $i
done[macg@machome ~]$ sh test
111-tmp.txt
111.txt
22.txt
33.txt
LIST="rootfs usr data data2"
for d in $LIST; do
done
- shell笔记(二)——for循环
- C++笔记(二):for循环问题
- Linux shell学习(五)——for循环
- shell 编程——for in 循环
- shell 编程——for in 循环
- shell ——for in 循环
- shell 编程——for in 循环
- shell 编程——for in 循环
- Shell编程—for循环总结【转】
- shell学习二十五--for循环
- scala 学习(二)——for循环break
- Linux Shell 笔记二(循环结构)
- shell笔记(三)——其他循环结构
- Linux笔记(53)——for循环
- Linux Shell 脚本应用——使用for循环
- shell练习题之for循环——读取/var目录
- for 循环 and while 循环(二)
- shell循环1.for循环
- 应聘时漂亮
- USB Camera 预览 拍照
- 页面布局技术easyui&zTree
- IO流使用需求:
- U-Boot编译过程完全分析
- shell笔记(二)——for循环
- 外链的建设
- java中J2EE J2SE J2ME 的区别
- IO流总结3
- OC学习之Objective-C的内存管理
- 在c++中如何控制小数位数
- UNIX网络编程——带外数据小结
- hdu40069(优先级队列)
- Solution:Unable to load perf ormance pack. Using Java I/O instead. Please ensure that wlntio.dll