Using two variables in a loop to access two diff file types in the same folder shell scripting linux

廉价感情. 提交于 2019-12-24 19:54:44

问题


I am trying to call two diff files types in a loop.

I have a1.in-a10.in files and b1.out-b10.out files. I wanna access both files simultaneously. I dont want to use nested loops but simultaneously.

for f1,f2 `ls *.in` `ls *.out`;do
echo "$f1 $f2"
done

I get f1 and f2 not valid identified error


回答1:


You can process this with essentially the same command as you did with your last question. Just remove the extra arguments and the Java command.

for num in $(seq 1 10);
    do echo a$num.in b$num.out; # processing command here
done;



回答2:


One way is this (here assuming bash):

$ touch a{1..10}.in b{1..10}.in

$ ls
a10.in  a2.in  a4.in  a6.in  a8.in  b10.in  b2.in  b4.in  b6.in  b8.in
a1.in   a3.in  a5.in  a7.in  a9.in  b1.in   b3.in  b5.in  b7.in  b9.in

$ for i in {1..10}; do echo a$i.in b$i.in; done
a1.in b1.in
a2.in b2.in
a3.in b3.in
a4.in b4.in
a5.in b5.in
a6.in b6.in
a7.in b7.in
a8.in b8.in
a9.in b9.in
a10.in b10.in

Here I'm just echoing the strings but you can use any command you like, diff, cat, etc instead of echo



来源:https://stackoverflow.com/questions/17097098/using-two-variables-in-a-loop-to-access-two-diff-file-types-in-the-same-folder-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!