Back to All

Problems using bash script in a CGC tool

Hello!
I have created a tool based on Samtools, I used the following repository: images.sbgenomics.com/marouf/samtools:1.3 which i found in the public Samtools app.
My goal is to make a tool that extracts the sample's name starting from .bam files, because i need them in the subsequent tool of my workflow.
As input i'll give to samtools an array composed by two files (1 Tumor and 1 Normal tissue from the same patient), and i want the tool to discriminate if the name extracted belongs to a tumor sample, or a normal sample.
So i wrote this bash script:
for i in /sbgenomics/Projects//.bam; do if [ `/opt/samtools-1.3/samtools view -H $i | grep '^@RG' | sed "s/.SM:............-(...)-./\1/g" | uniq` == "01A" ]; then /opt/samtools-1.3/samtools view -H $i | grep '^@RG' | sed "s/.SM:([^\t])./\1/g" | uniq; fi; done > tumor_name.txt

In other words, for every bam in my folder (1 tumor and 1 normal) it should extract the 3 numbers that identify the sample type, compare them to "01A" (which is specific for tumor samples), if they are correct then it prints the entire sample name and puts it into a file.

it returns the subsequent error log:

2017-10-13T18:10:34.886498415Z sh: 1: [: 11A: unexpected operator
2017-10-13T18:10:34.891268970Z sh: 1: [: 01A: unexpected operator

11A and 01A should be the "3 number ID" extracted as the first argument of the "if loop" (01A for the tumor sample, 11A for the normal sample), so apparently it seems that the if statement doesn't like them as arguments, as well as the opened square brackets.
In the end, the tool returns me the file tumor_name.txt which is unfortunately empty (reasonably because the if statement didn't work).

I thought that should have put #!/bin/bash before the "for loop" as my script is using bash commands.
However when i use #!/bin/bash the standard output command ">" stops working, and this doesn't make sense to me.
I tried a simple bash script to test it: #!/bin/bash echo 123 > file.txt
and it doesn't work, while echo 123 > file.txt (without #!/bin/bash) works perfectly.

Any help?
Am i missing something in order to use bash scripts in CGC?

Thank you very much in advance!