Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7656883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:53:03+00:00 2026-05-31T12:53:03+00:00

then, elif, else statement that I have programmed in a bash script. I know

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:53:04+00:00Added an answer on May 31, 2026 at 12:53 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a bash script that gets three paths based on input parameters
Every now and then I receive a Word Document that I have to display
I have used the & command before to make a script run another script
I have created a Bash script and I have some rather fugly arguments validation.
i have written this python program. whenever i run the script using parameters like
Every now and then, I bump into syntax that I've seen before, but never
I'm running a script to feed an exe file a statement like below: for
My dilemma: I'm passing my function a string that I need to then perform
I have a master shell script which called a child script for each iteration
I have a simple python telnet based chat server that lacks the functionality for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.