目录
shell命令行中使用循环:
[root@localhost tmp]# for i in `ls -1`; do echo $i; done
a.txt
b.txt
c.txt
[root@localhost tmp]# for i in $(ls -1); do echo $i; done
a.txt
b.txt
c.txt
[root@localhost tmp]#
[root@localhost tmp]# for i in {1..10}; do echo $i; done
1
2
3
4
5
6
7
8
9
10
[root@localhost tmp]#
[root@localhost tmp]# start=1; end=10; for((i=$start;i<=$end;i++)); do echo "test: "$i; done
test: 1
test: 2
test: 3
test: 4
test: 5
test: 6
test: 7
test: 8
test: 9
test: 10
[root@localhost tmp]#
[root@localhost tmp]# start=1; end=10; for((i=start;i<=end;i++)); do echo "test: "$i; done
test: 1
test: 2
test: 3
test: 4
test: 5
test: 6
test: 7
test: 8
test: 9
test: 10
[root@localhost tmp]#
[root@localhost tmp]# i=1; max=10; while(($i<$max)); do echo "test: "$i; i=$(($i + 1)); done
test: 1
test: 2
test: 3
test: 4
test: 5
test: 6
test: 7
test: 8
test: 9
[root@localhost tmp]#
[root@localhost tmp]# i=1; max=10; while(true); do echo "test: "$i; i=$(($i + 1)); if [ $i -gt $max ];then break; fi; done
test: 1
test: 2
test: 3
test: 4
test: 5
test: 6
test: 7
test: 8
test: 9
test: 10
[root@localhost tmp]#
循环拷贝文件、按照固定格式(文件名后缀包含固定几位数字、不足时补0)输出文件名:
[root@localhost filecopy_200kb_test]# ls -1
a.txt
[root@localhost filecopy_200kb_test]#
[root@localhost filecopy_200kb_test]# for ((i=1;i<=10;i++)); do printf "cp a.txt 200kb_%05d.txt\n" $i; done
cp a.txt 200kb_00001.txt
cp a.txt 200kb_00002.txt
cp a.txt 200kb_00003.txt
cp a.txt 200kb_00004.txt
cp a.txt 200kb_00005.txt
cp a.txt 200kb_00006.txt
cp a.txt 200kb_00007.txt
cp a.txt 200kb_00008.txt
cp a.txt 200kb_00009.txt
cp a.txt 200kb_00010.txt
[root@localhost filecopy_200kb_test]#
[root@localhost filecopy_200kb_test]# for ((i=1;i<=10;i++)); do $(printf "cp a.txt 200kb_%05d.txt\n" $i); done
[root@localhost filecopy_200kb_test]#
[root@localhost filecopy_200kb_test]# ls -1
200kb_00001.txt
200kb_00002.txt
200kb_00003.txt
200kb_00004.txt
200kb_00005.txt
200kb_00006.txt
200kb_00007.txt
200kb_00008.txt
200kb_00009.txt
200kb_00010.txt
a.txt
[root@localhost filecopy_200kb_test]#
[root@localhost tmp]# str=""; start=133800; max=5; for((i=1;i<=max;i++)); do str+="DATA_OBJ#="$(($start+$i))" OR"; echo $str; done
DATA_OBJ#=133801 OR
DATA_OBJ#=133801 ORDATA_OBJ#=133802 OR
DATA_OBJ#=133801 ORDATA_OBJ#=133802 ORDATA_OBJ#=133803 OR
DATA_OBJ#=133801 ORDATA_OBJ#=133802 ORDATA_OBJ#=133803 ORDATA_OBJ#=133804 OR
DATA_OBJ#=133801 ORDATA_OBJ#=133802 ORDATA_OBJ#=133803 ORDATA_OBJ#=133804 ORDATA_OBJ#=133805 OR
[root@localhost tmp]#
[root@localhost tmp]# str=""; start=133800; max=5; for((i=1;i<=max;i++)); do str="DATA_OBJ#="$(($start+$i))" OR"; echo $str; done
DATA_OBJ#=133801 OR
DATA_OBJ#=133802 OR
DATA_OBJ#=133803 OR
DATA_OBJ#=133804 OR
DATA_OBJ#=133805 OR
[root@localhost tmp]#
中间过程不输出、仅输出最终的拼装结果:
[root@localhost tmp]# str=""; start=133800; max=5; for((i=1;i<=max;i++)); do str+="DATA_OBJ#="$(($start+$i))" OR"; done; echo $str
DATA_OBJ#=133801 ORDATA_OBJ#=133802 ORDATA_OBJ#=133803 ORDATA_OBJ#=133804 ORDATA_OBJ#=133805 OR
[root@localhost tmp]#
换个写法:
拼装的中间过程也输出:
[root@localhost home]# str=" OR DATA_OBJ#="; start=133100; ret=""; for i in {1..10}; do ret+=$str$(($start + $i)); echo $ret; done;
中间过程不输出,拼装完毕后,最终一次性输出最终结果:
[root@localhost home]#
[root@localhost home]# str=" OR DATA_OBJ#="; start=133100; ret=""; for i in {1..10}; do ret+=$str$(($start + $i)); done; echo $ret;
shell脚本文件中使用循环:
#vim loop.sh
#!/bin/sh
info=""
for((i=0;i<10;i++))
do
info=$info"a"
done
echo $info
info2=""
for i in `seq 10`
do
info2=$info2"b"
done
echo $info2
info3="";
for i in {1..10}
do
info3=$info3"c"
done
echo $info3
info4=""
pos=1
max=10
while [ $pos -le $max ]
do
info4=$info4"d"
pos=`expr $pos + 1`
done
echo $info4
info5=""
pos=1
max=10
while(($pos<=10))
do
info5=$info5"e"
pos=$(($pos + 1))
done
echo $info5
[root@localhost home]# ./loop.sh
aaaaaaaaaa
bbbbbbbbbb
cccccccccc
dddddddddd
eeeeeeeeee
[root@localhost home]#