Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add file by (*) star character to variable in for loop

I have a folder structure where two files are in a folder. The files have long names, yet are distinguished by R1 and R2. Note I am running this over many folders using the for loop but keeping it simple for this example. I am running a loop and am wonder how to correctly call the files with a (*) star character to autocomplete without having to type in all file name. My attempt is below:

#!/bin/bash

for item in Folder_Directory:
do
forward=$item/*R1*
reverse=$item/*R2*
bbmap.sh ref=reference.fna in1=$forward in2=$reverse outu=Unmapped.fasta
done

The output I am getting is an error because the variable is not identifying the desired file:

Error:

align2.BBMap build=1 overwrite=true fastareadlen=500 ref=reference.fna 
in1=Folder_Dictory/*R1* in2=Folder_Dictory/*R2* outu=Folder_Dictory/Unmapped.fastq

In this example I could autocomplete the files, however, when I expand this loop to include multiple folders that is no longer ideal. Autocompleting using (*) characters was my first approach, any other suggestions or fixes to my issue are greatly appreciated.

like image 385
Cody Glickman Avatar asked Dec 01 '25 11:12

Cody Glickman


1 Answers

The problem is that the shell sees in1=Folder_Dictory/*R1* and notices that there are no files which match the glob with the literal in1= prefix, and so the wildcard does not get expanded at all.

You probably want to evaluate the wildcard before passing it to the command, like for instance

for item in Folder_Directory:
do
  forward=$item/*R1*
  reverse=$item/*R2*
  bbmap.sh ref=reference.fna in1="$(echo $forward)" in2="$(echo $reverse)" outu=Unmapped.fasta
done

This will of course still be erratic if the wildcard expands to more than one file.

like image 147
tripleee Avatar answered Dec 04 '25 05:12

tripleee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!