GB44444
GB44444

Reputation: 57

Calling a variable from another file into a bash script

I've trawled through similar questions to this, but can't quite find something that works, and wondered if you could kindly help.

I am trying to execute the following bash script:

#!/bin/bash
for i in {0..10000}
do
mysql --user=genome --host=genome-mysql.cse.ucsc.edu -A -D hg19 -e '
Select 
 S.name as rsID,
 S.chrom,
 S.chromEnd,
 S.chromStart,
 K.txStart,
 K.txEnd, 
 G.geneSymbol,
 G.kgID 
from snp138 as S
left join knownGene as K on
 (S.chrom=K.chrom and not(K.txEnd+1000000<S.chromStart or S.chromEnd+1000000<K.txStart))
right join kgXref as G on
 (K.name=G.kgID)
where
  S.name in ("snp_permutation"$i".txt")'| awk -F '|' '{print $1}' | sed 1d | awk '{print $7}' | sort -u > permutation"$i"_genelist.txt

Essentially, I have 10,000 files called snp_permutation"$i".txt, where i runs from 1 to 10,000. Each of these files is just a single line, and its only content looks something like:

rs16574876

For this script to work, I need the actual content of the files (e.g. "rs16574876" to go between the quotation marks in S.name in ("snp_permutation"$i".txt")', rather than the name of the file itself.

Do I need to use source or export for this?

Thank you for your help.

Upvotes: 0

Views: 81

Answers (1)

glenn jackman
glenn jackman

Reputation: 246807

How about this

#!/bin/bash

template=$(cat <<'END'
    Select 
        S.name as rsID,
        S.chrom,
        S.chromEnd,
        S.chromStart,
        K.txStart,
        K.txEnd, 
        G.geneSymbol,
        G.kgID 
    from snp138 as S
    left join knownGene as K on
        (S.chrom=K.chrom and not(K.txEnd+1000000<S.chromStart or S.chromEnd+1000000<K.txStart))
    right join kgXref as G on
        (K.name=G.kgID)
    where
        S.name in (%s)
END
)

for (( i=0; i <= 10000; i++ )); do
    printf -v sql "$template" "$(< "snp_permutation${i}.txt")"
    mysql --user=genome --host=genome-mysql.cse.ucsc.edu -A -D hg19 -e "$sql" |
      awk -F '|' 'NR > 1 {split($1, a, " "); print a[7]}' | 
      sort -u > "permutation${i}_genelist.txt"
done

This uses $(<file) which is a bash builtin for $(cat file)

Upvotes: 1

Related Questions