简体   繁体   中英

Wildcard in filename not working in bash

I know there are many (MANY!) similar questions in here but I haven't been able to find one that solves my problem. I have the following script:

#!/bin/bash

/* MAIN_CLASS and other declarations here */

numOfDatasets=10

for (( i=1; i <= $numOfDatasets; ++i )) do
    DATASET_FILE="C:\path\to\file\name_"$i"_100.fa" 
    QUERY_FILE="C:\path\to\file\name_${i}_100_p"*".fasta"
    mvn exec:java -Dexec.mainClass="${MAIN_CLASS}" -Dexec.args="${DATASET_FILE} ${QUERY_FILE}"
done

But I get the error

java.io.FileNotFoundException: C:\\path\\to\\file\\name_1_100_p*.fasta (The filename, directory name, or volume label syntax is incorrect)

Some examples of the strings I would like to be assigned to QUERY_FILE in different iterations are name_1_100_p95.fasta , name_2_100_p79.fasta , name_3_100_p89.fasta etc. A command like

echo "name_"*"_100_p"*".fasta"

directly in the terminal works.

Try using an inner for loop which iterates through the results of the wildcard search:

for QUERY_FILE in "C:\path\to\file\name_${i}_100_p"*".fasta"; do
   /* do stuff with $QUERY_FILE */
done

You are assigning QUERY_FILE a string value but I think you want it to be the result of running a command instead.
Try something like this:

QUERY_FILE=`ls "C:\path\to\file\name_${i}_100_p"*".fasta" | head -1`

the head -1 is if you want the first file matched from the * , you can modify that to suit your specific needs.

Full example:

#!/bin/bash

/* MAIN_CLASS and other declarations here */

numOfDatasets=10

for (( i=1; i <= $numOfDatasets; ++i )) do
    DATASET_FILE="C:\path\to\file\name_"$i"_100.fa" 
    QUERY_FILE=`ls "C:\path\to\file\name_${i}_100_p"*".fasta" | head -1`  
    mvn exec:java -Dexec.mainClass="${MAIN_CLASS}" -Dexec.args="${DATASET_FILE} ${QUERY_FILE}"
done

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