Search code examples
bashif-statementqiime

if then bash statement does not work with qiime commands


I just started to bash and I have been stuck for sometime on a simple if;then statement. I use bash to run QIIME commands which are written in python. These commands allow me to deal with microbial DNA. From the raw dataset from the sequencing I first have to first check if they match the format that QIIME can deal with before I can proceed to the rest of the commands.

module load QIIME/1.9.1-foss-2016a-Python-2.7.11
echo 'checking mapping file and demultiplexing'
validate_mapping_file.py -m $PWD/map.tsv -o $PWD/mapcheck > tmp.txt
n_words=`wc -w tmp.txt`
echo "n_words:"$n_words
if [ n_words = '9 temp.txt' ];then
split_libraries_fastq.py -i $PWD/forward_reads.fastq.gz -b $PWD/barcodes.fastq.gz -m $PWD/map.tsv -o $PWD/demultiplexed
else
  echo 'Error(s) in map'
  exit 1
fi

If the map is good I expect the following output (9 words):

No errors or warnings were found in mapping file. 

If it is bad (16 words):

Errors and/or warnings detected in mapping file.  Please check the log and html file for details.

I want to used this output to condition the following commands split_libraries_fastq.py.

I tried many different version of the if;then statement, asked help around but nothing seems to be working. Anyone of you had an idea of why the 'then' command is not ran? Also I run it through a cluster.

Here is the output when my map is good, the second command is not ran:

checking mapping file and demultiplexing
n_words:9 tmp.txt
Error(s) in map

Thanks


Solution

  • Review shell syntax, in particular double quotes and parameter expansion. You need a dollar to expand n_words and double quotes to make it a single string notwithstanding the embedded space. For example:

    if [ "$n_words" = '9 temp.txt' ]; then
        echo "good"
    else
        echo "bad"
    fi
    

    Alternatively, consider omitting the file name and doing an integer comparison:

    n_words=`wc -w < tmp.txt`
    echo "n_words: $n_words"
    if [ "$n_words" -eq 9 ]; then
    #...
    

    Finally, let me warn you that counting the number of words is a bad hack, as innocent changes in the Python script may break you shell script. I'm not familiar with Qiime, but they should provide a meaningful exit status. Try:

    validate_mapping_file.py -m $PWD/map.tsv -o $PWD/mapcheck > /dev/null
    exit_status=$?
    echo "exit status: $exit_status"