[英]Looping using the name of directories in bash
example files: 示例文件:
sample1_day1/ sample1_day1 /
sample1.str
sample1.yy
sample1_ABC.log
sample2_day1/ sample2_day1 /
sample2.str
sample2.yy
sample2_ABE.log
sample3_day1/ sample3_day1 /
sample3.str
sample3.yy
sample3_ASC.log
I have used this to loop through files: 我用它来遍历文件:
for i in `echo *$FILTER | sed 's/'$FILTER'//'`;do
"something here";
done
This gets the "identifier", "sampleX" in this case but when I used it to loop through folders, it failed. 在这种情况下,这将获得“标识符”,“ sampleX”,但是当我使用它遍历文件夹时,它失败了。
I need to assign the $i
as the "name tag" for easy naming of output and input files needed by a program like: 我需要将
$i
分配为“名称标签”,以方便命名程序所需的输出和输入文件:
for i in something; do
"program --input $i.str --input2 $i.yy --out $i\_clean.txt"
done
I wanted my bash script to get the name of "sampleX's" given the folders (iesample1_day1, etc) and make a list of sampleX's, use this identifier to cd
(eg, cd $i\\_day1
) to the folder and do some analyses in that folder, subsequently naming output and input accordingly. 我希望我的bash脚本在给定文件夹(即sample1_day1等)的情况下获取“ sampleX's”的名称,并列出sampleX的列表,使用此标识符将
cd
(例如cd $i\\_day1
) cd $i\\_day1
文件夹并在其中进行一些分析。该文件夹,随后命名输出和输入。
In short, How do I get just the sample1 and sample2 and sample3 and represent them as i? 简而言之,我如何只获取sample1和sample2和sample3并将它们表示为i?
Thanks 谢谢
for i in */ ; do
"program --input $i.str --input2 $i.yy --out $i\_clean.txt"
done
If it is just sample1, sample2, sample3 then this will also work- 如果只是sample1,sample2,sample3,那么这也将起作用-
for i in {1..3}
do
"program --input sample"$i".str --input2 sample"$i".yy --out sample"$i"\_clean.txt"
done
Little modified version of @fedterzi's answer. @fedterzi的答案的小修改版本。 This one helps only to navigate inside sample _day1 or sample _day* even if you have directory with different name present.
即使您存在具有不同名称的目录,此选项也仅有助于在示例_day1 或示例 _day *内部进行导航。
#!/bin/bash
for i in sample*_day1/ ; do
cd $i
"program --input $i.str --input2 $i.yy --out $i\_clean.txt"
done
After reading and reading for almost a few hours, I realized what was missing: the "/" that denotes its property. 在阅读了将近几个小时之后,我意识到缺少的东西:表示其属性的“ /”。
But of course. 但是当然。 And then I also noticed this in the answers of @Parthiban and @fedterzi (thanks guys!).
然后我在@Parthiban和@fedterzi的回答中也注意到了这一点(谢谢大家!)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.