简体   繁体   中英

Looping using the name of directories in bash

example files:

sample1_day1/

sample1.str

sample1.yy

sample1_ABC.log

sample2_day1/

sample2.str

sample2.yy

sample2_ABE.log

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.

I need to assign the $i as the "name tag" for easy naming of output and input files needed by a program like:

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.

In short, How do I get just the sample1 and sample2 and sample3 and represent them as 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-

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. This one helps only to navigate inside sample _day1 or sample _day* even if you have directory with different name present.

#!/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!).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM