简体   繁体   中英

bash - assigning output of find to variable while looping through list

I'm new to shell scripting. In bash, I'm trying to assign the output of find to a new variable while looping through a list.

for i in {25,27}; do
     r1=$(find $i*R1_001.fastq.gz)
     r2=$(find $i*R2_001.fastq.gz)
done

What I want to happen is for the compute to assign a file name to r1 and r2. For instance:

$ echo $r1

25-NVB206M02_S27_R1_001.fastq.gz

However, the computer interprets this as if the * is not a wildcard. I get an error that states:

find: `25*R1_001.fastq.gz': No such file or directory

Thank you for any advice you can provide.

If you want to use find you need something like:

r1=$(find . -name "${i}*R1_001.fastq.gz" | sed 's#^.*/##')

or, better, you can use:

r1="${i}*R1_001.fastq.gz"

Be aware that you may match 0 or more files with this. If multiple files match, for instance, if i=2 then r1 and r2 would match 2-NVB... , 20-NVB... , 21-NVB... , 200-NVB... , and so on. You probably are only expecting the variable to hold a single file name. Try to tighten up what you are matching on.

If no files match r1/r2 may still be not empty. Example:

(pi19 692) $ ls "*foo*"
ls: *foo*: No such file or directory
(pi19 693) $ r1="*foo*"
(pi19 694) $ echo $r1
*foo*

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