相关文章推荐
选择频道 点击选择频道
全部
Ada助手

shell for循环两个变量实现

2012-08-06
我想实现for里面如下代码,可语法错误,有没有类似的实现方式呢
#!/bin/bash
src1=/home/ubuntu/quinton/search/test1
src2=/home/ubuntu/quinton/search/test2
for file hi in $src1 $src2
echo here are $file $hi
5 打赏
全部评论
[Quote=引用 4 楼 的回复:]
引用 2 楼 的回复:
引用 1 楼 的回复:
1 #!/bin/bash
3 src1=/home/ubuntu/quinton/search/test1
4 src2=/home/ubuntu/quinton/search/test2
6 for file in $src1; do
8 echo here are $file
9 d……
[/Quote]
看来只有用小trick来处理,而用for循环里两个变量的思路不行
2012-08-07
[Quote=引用 2 楼 的回复:]
引用 1 楼 的回复:
1 #!/bin/bash
3 src1=/home/ubuntu/quinton/search/test1
4 src2=/home/ubuntu/quinton/search/test2
6 for file in $src1; do
8 echo here are $file
9 done
10 for……
[/Quote]既然有这么好的对应关系,循环一次就够了。
$ cd /tmp
$ mkdir test{1..2}
$ touch test1/file{1..10}
$ touch test2/hi{1..10}
$ for file in test1/*;do echo here are $file `echo $file |sed 's/test1/test2/g;s/file/hi/g'`;done
here are test1/file1 test2/hi1
here are test1/file10 test2/hi10
here are test1/file2 test2/hi2
here are test1/file3 test2/hi3
here are test1/file4 test2/hi4
here are test1/file5 test2/hi5
here are test1/file6 test2/hi6
here are test1/file7 test2/hi7
here are test1/file8 test2/hi8
here are test1/file9 test2/hi9
2012-08-07
[[email protected] sh_project]$ ./main.sh   
1,55555
2,5555
3,6666
[[email protected] sh_project]$ cat a.txt
[[email protected] sh_project]$ cat b.txt
55555
[[email protected] 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
2012-08-07
[Quote=引用 1 楼 的回复:]
1 #!/bin/bash
3 src1=/home/ubuntu/quinton/search/test1
4 src2=/home/ubuntu/quinton/search/test2
6 for file in $src1; do
8 echo here are $file
9 done
10 for……
[/Quote]
不是这个意思,我想在一个for循环里面用到$file和$hi这两个变量,这两个变量一一对应,即在第一次循环里file1代替hi1,第二次循环里file2代替hi2......
2012-08-06
1 #!/bin/bash
3 src1=/home/ubuntu/quinton/search/test1
4 src2=/home/ubuntu/quinton/search/test2
6 for file in $src1; do
8 echo here are $file
9 done
10 for hi in $src2; do
12 echo here are $hi
13 done
分开写,可以的。
2012-08-06
——到底了——
说点什么…
 
推荐文章