简体   繁体   English

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

[英]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: 我正在尝试执行以下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

Essentially, I have 10,000 files called snp_permutation"$i".txt, where i runs from 1 to 10,000. 本质上,我有10,000个名为snp_permutation“ $ i” .txt的文件,我的运行范围是1到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 (eg "rs16574876" to go between the quotation marks in S.name in ("snp_permutation"$i".txt")', rather than the name of the file itself. 为了使该脚本正常工作,我需要文件的实际内容 (例如,“ rs16574876”要位于(“ snp_permutation” $ i“ .txt”)'中S.name的引号之间,而不是文件名本身。

Do I need to use source or export for this? 我需要为此使用源或导出吗?

Thank you for your help. 谢谢您的帮助。

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) 这使用$(<file)这是$(cat file)内置bash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM