将另一个文件中的变量调用到bash脚本中

时间:2018-04-18 16:52:33

标签: bash variables export

我已经通过类似的问题对此进行了搜索,但是找不到有效的方法,并且想知道你是否可以提供帮助。

我正在尝试执行以下bash脚本:

#!/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

基本上,我有10,000个名为snp_permutation“$ i”.txt的文件,其中我从1到10,000运行。这些文件中的每一个都只是一行,其唯一的内容类似于:

rs16574876

为了让这个脚本工作,我需要文件的实际内容(例如“rs16574876”在S.name中的引号之间(“snp_permutation”$ i“.txt”) )',而不是文件本身的名称。

我是否需要使用源或导出?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

这个怎么样

#!/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

这使用$(<file),它是内置于$(cat file)

的bash