日期:2014-05-16  浏览次数:20821 次

shell for循环两个变量实现
我想实现for里面如下代码,可语法错误,有没有类似的实现方式呢

#!/bin/bash

src1=/home/ubuntu/quinton/search/test1
src2=/home/ubuntu/quinton/search/test2

for file hi in $src1 $src2
do
  echo here are $file $hi
done


------解决方案--------------------
1 #!/bin/bash

3 src1=/home/ubuntu/quinton/search/test1
4 src2=/home/ubuntu/quinton/search/test2

6 for file in $src1; do
7
8 echo here are $file
9 done
 10 for hi in $src2; do
 11
 12 echo here are $hi
 13 done


分开写,可以的。
------解决方案--------------------
Python code
[liangdong@bb-browser-test00.vm.baidu.com sh_project]$ ./main.sh   
1,55555
2,5555
3,6666
4,
5,
[liangdong@bb-browser-test00.vm.baidu.com sh_project]$ cat a.txt 
1
2
3
4
5
[liangdong@bb-browser-test00.vm.baidu.com sh_project]$ cat b.txt 
55555
5555
6666
[liangdong@bb-browser-test00.vm.baidu.com sh_project]$ cat main.sh 
#!/bin/bash

awk 'BEGIN{cnt_a=0;cnt_b=0;}FNR==NR{a[cnt_a++]=$0;next} {b[cnt_b++]=$0} END{cnt=0;cnt_max=cnt_a<cnt_b ? cnt_b:cnt_a;for(;cnt<cnt_max;++cnt){print a[cnt]","b[cnt]}}' a.txt b.txt

------解决方案--------------------
探讨

引用:

1 #!/bin/bash
2
3 src1=/home/ubuntu/quinton/search/test1
4 src2=/home/ubuntu/quinton/search/test2
5
6 for file in $src1; do
7
8 echo here are $file
9 done
10 for……

……