Bash Scripting : How to loop over X number of file

2019-08-01 09:28发布

So I have a program written in C that takes in some parameters: calling it allcell some sample parameters: -m 1800 -n 9 the files being analyzed: cfdT100-0.trj, cfdT100-1.trj, cfdT100-2.trj, cfdT100-3.trj, ... cfdT100-19.trj file being fed: template.file out file: result.file

 $ allcell -m 1800 -n 9 cfdT100-[0-19].trj < template.file > result.file

But when I htop, I see that only cfdT100-0.trj, cfdT100-1.trj and cfdT100-9.trj are being read. How do I make the shell read all the files from 0-19 ?

Additionally, when I write a script file to automate this, how should I enclose the line? Will this work:

"$($ allcell -m 1800 -n 9 cfdT100-[0-19].trj < template.file > result.file)"

2条回答
老娘就宠你
2楼-- · 2019-08-01 09:50

use recursion function for infinite loop

a()
{
echo "apple"
a
}
a

This the will make a infinite loop

查看更多
霸刀☆藐视天下
3楼-- · 2019-08-01 09:53

I believe you want to change your glob expression to cfdT100-{0..19}.trj instead.

neech@nicolaw.uk:~ $ echo cfdT100-{0..19}.trj
cfdT100-0.trj cfdT100-1.trj cfdT100-2.trj cfdT100-3.trj cfdT100-4.trj cfdT100-5.trj cfdT100-6.trj cfdT100-7.trj cfdT100-8.trj cfdT100-9.trj cfdT100-10.trj cfdT100-11.trj cfdT100-12.trj cfdT100-13.trj cfdT100-14.trj cfdT100-15.trj cfdT100-16.trj cfdT100-17.trj cfdT100-18.trj cfdT100-19.trj

Your quoting on the scripted version looks acceptable. Just change the glob.

查看更多
登录 后发表回答