brucezepplin
brucezepplin

Reputation: 9752

use bash commandline variables in mysql query

Hi I have the following script:

#!/bin/sh
mysql --user=genome --host=genome-mysql.cse.ucsc.edu -A hg19 -D hg19 -e "select distinct c.name,c.transcript from  (select distinct kgID,genesymbol,refseq from kgXref where genesymbol = '"${1}"') a inner join (select * from knownGene) b on a.kgID = b.name inner join (SELECT distinct name, transcript,chromStart,chromEnd, substr(peptides,1,1) as ref_pep,substr(peptides,3,1) as mut_pep FROM snp141CodingDbSnp) c on a.refseq = c.transcript" > ${1}id.txt

where I use it like:

./snp_id_list.sh <myGene>

However I get:

./snp_id_list.sh: 3: Syntax error: Unterminated quoted string

to which I don't think I am escaping the double quotes correctly around the first instance of $1 (the one time I call it within the mysql syntax)

Upvotes: 0

Views: 44

Answers (1)

Fabian Schmengler
Fabian Schmengler

Reputation: 24551

The double quotes are not needed at all. The following should work:

mysql --user=genome --host=genome-mysql.cse.ucsc.edu -A hg19 -D hg19 -e \
    "select distinct c.name,c.transcript from \
    (select distinct kgID,genesymbol,refseq from kgXref where genesymbol = '${1}') a \
    inner join (select * from knownGene) b on a.kgID = b.name \
    inner join (SELECT distinct name, transcript,chromStart,chromEnd, substr(peptides,1,1) as ref_pep,substr(peptides,3,1) as mut_pep \
    FROM snp141CodingDbSnp) c on a.refseq = c.transcript" > ${1}id.txt

(I made it a multi line statement for sake of readability)

Upvotes: 1

Related Questions