我想实现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=引用 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
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
分开写,可以的。
引用 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循环里两个变量的思路不行
引用 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
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......
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
分开写,可以的。