简体   繁体   中英

Globbing a filename and then saving to a variable

I've got some files in a directory with a standard format, I'm looking to use a txt file with part of the filenames to extend them through * then finally add on a.gz tag as an output

For example, a file called 1.SNV.111-T.vcf in my directory, I have 111-T in my txt file.

#!/bin/bash
while getopts f: flag
do
        case "${flag}" in
                f) file=${OPTARG};;
esac
done

while IFS="" read -r p || [ -n "$p" ]
do
        vcf="*${p}.vcf"

        bgzip -c ${vcf} > ${vcf}.gz

done < $file

This will successfully run bgzip but actually save the output to be:

'*111-T.vcf.gz'

So adding.gz at the end has "deactivated" the * character, as pointed out by Barmar this is because there isn't a file in my directory called 1.SNV.111-T.vcf.gz so the wildcard is inactivated, please can anyone help?

I'm new to bash scripting but I assume there must be some way to save the "absolute" value of my vcf variable so that once it has found a match the first time, it's now a string that can be used downstream? I really cant find anything online.

The problem is that wildcards are only expanded when they match an existing file. You can't use a wildcard in the filename you're trying to create.

You need to get the expanded filename into the vcf variable. You can do it this way:

vcf=$(echo *"$p.vcf")
bgzip -c "$vcf" > "$vcf.gz"

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