0

I'm trying to make a bash script to use a program called bamql. The execution of bamql from the terminal would be something like this:

bamql -f <file_in> 'chr(<name>) & nt_exact(<number>,<letter>)' -o <file_out>

<file_in> is the input file to the program <name> is the name of the chromosome of the organism that I am studying <number> It is an integer indicating the position that I want to study <letter> is a letter (A, C, G or T) indicating the mutation that I want to study <file_out> is the output file of the program

An example of BAMQL execution would be the following

bamql -f BC02.sorted.bam 'chr(PB2) & nt_exact(482,T)' -o PB2_482_T.bam

So what I want is to make a bash script to be able to execute BAMQL with variables, like this:

bamql -f ${FILE_IN} 'chr(${CHRN}) & nt_exact(${SNV})' -o ${FILE_OUT}

The problem is that the previous code does not work, it gives me the error "Unexpected character", and the variables ${CHRN} and ${SNV} do not take their value. I have also tried to enclose the whole 'chr(${CHRN}) & nt_exact(${SNV})' part in an array and run the code:

args=('chr(${CHRN}) & nt_exact(${SNV})')

bamql -f ${FILE_IN} "${args[@]}" -o ${FILE_OUT}

but again the variables ${CHRN} and ${SNV} do not take their value.

I would be grateful if someone gives me some suggestion.

Arturo
  • 11
  • 2
  • 1
    `bamql` _has no way of knowing_ if single or double quotes were used on its command line; they all just turn into C strings. There's no reason for you to keep using the same type of quotes the tool's original documentation suggests if those quotes don't behave the way you want with respect to how contents within them are expanded. – Charles Duffy Jan 05 '23 at 00:00
  • 1
    Also, you can change quoting types within a single string. That means you can run `bamql -f "$file_in" 'chr('"$chrn"') & nt_exact('"$snv"')' -o "$file_out"`, having the constants single-quoted and the expansions double-quoted. (**Never** leave expansions unquoted; see [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable)). – Charles Duffy Jan 05 '23 at 00:02
  • 1
    btw, the examples above using lower-case variable names is also deliberate: [POSIX specifications](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html) specify that operating-system-vendor-provided tools use only all-caps names for variables that modify or reflect their operational status, reserving lowercase names for application use; using lowercase names thus prevents changing a variable that impacts how OS-provided tools work by mistake (the classic example is `for PATH in */; do ...` and then wondering why you can't run any programs anymore; `for path in` avoids it). – Charles Duffy Jan 05 '23 at 00:04
  • 1
    Another note: http://shellcheck.net/ is your friend; it points out both missing quoting and places where quotes prevent variables from expanding properly. – Charles Duffy Jan 05 '23 at 00:05
  • Thanks @CharlesDuffy , your suggestion `bamql -f "$file_in" 'chr('"$chrn"') & nt_exact('"$snv"')' -o "$file_out"` worked. I'll use the [link]shellcheck.net website to check the rest of my code. – Arturo Jan 05 '23 at 21:25

0 Answers0