Linux – If then elif then else statement in bash

bashif statementlinuxterminal

then, elif, else statement that I have programmed in a bash script. I know that it works because I can run the same command in the terminal interface and see that it is doing what I want it to do. However when I run it in a script it seems to always jump to the else statement and not detect anything. Can anybody help explain why this is so? Here is my script code:

if [ -e "$1" ]
then
    for line in `samtools view -H $1`
    do
    if [[ "$line" ==  *NCBI-Build-36\.1* ]]
    then 
            echo "hg18"
            break
    elif [[ "$line" ==  *hg19* ]]
    then    
            echo "hg19"
            break
    else
            echo "Reference was not found, manual entry required: "
            read ans
            echo "$ans"
            break
    fi
    done
else
    echo -e "Usage: \e[1;32mreadRef.sh \e[1;36mbamfile.bam"
fi

No matter what file I plug in it always skips to the else and asks me for manual entry.

Here is the command I ran on terminal:

for line in `samtools view -H $bignormal`; do if [[ "$line" == *NCBI-Build-36\.1* ]]; then echo "YES - $line"; else echo "NO - $line"; fi; done

And the output is like this:

NO - @HD
NO - VN:1.0
NO - GO:none
NO - SO:coordinate
NO - @SQ
NO - SN:1
NO - LN:247249719
YES - AS:NCBI-Build-36.1
YES - UR:http://www.bcgsc.ca/downloads/genomes/9606/NCBI-Build-36.1/bwa_ind/genome/
NO - SP:Homo
NO - sapiens
.
.
.

Why is the script not detecting the string I am looking for, but it is in terminal?

EDIT:
I tried what Charles said, this is the output:

:+'[' -e /projects/rcorbettprj2/DLBCL/CNV/RG065/normal/A01440_8_lanes_dupsFlagged.bam ']'
::+samtools view -H  /projects/rcorbettprj2/DLBCL/CNV/RG065/normal/A01440_8_lanes_dupsFlagged.bam
:+for line in '`samtools view -H $1`'
:+case "$line" in
:+echo 'Reference was not found, manual entry required: '
Reference was not found, manual entry required: 
:+read ans

Best Answer

I think your code has a logic error nobody's spotted yet. I'm not sure, since you haven't told us what the script's supposed to be doing, but it looks to me like what you want is to ask for manual entry only if you don't find a match to either of your patterns anywhere in the output, but what you're actually doing is examining only the first word of output for a match. And from your sample output, the first word is "@HD", which doesn't match either pattern, so the script is doing exactly what I'd expect.

Now, assuming I'm right and that the point is to look for either pattern anywhere in the output, you can actually simplify things a bit. Mainly, you don't need the loop, you can just do a single comparison to look for the pattern in the entire output at once:

#!/bin/bash

if [ -e "$1" ]
then
    output="$(samtools view -H "$1")"
    if [[ "$output" ==  *NCBI-Build-36.1* ]]
    then 
            echo "hg18"
    elif [[ "$output" ==  *hg19* ]]
    then    
            echo "hg19"
    else
            read -p "Reference was not found, manual entry required: " ans
            echo "$ans"
    fi
    done
else
    echo -e "Usage: \e[1;32mreadRef.sh \e[1;36mbamfile.bam"
fi