New bash menu printing errors but closes too quickly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting New bash menu printing errors but closes too quickly
# 1  
Old 10-28-2015
New bash menu printing errors but closes too quickly

I am beginning to write a new version of the bash menu below. The previous version worked great and now when I start the updated bash it opens and a some lines print in the terminal and it closes quickly. I know there are errors but how can I see them or fix them, I tried set -x with the same result. Thank you Smilie.

Code:
#!/bin/bash

# VERSION2 FIXED INTRON BUG, ADDED NGS_SANGER MODULE, UPDATED MATRIX IMPORT

# clear batch entries
cd 'C:\Users\cmccabe\Desktop\annovar'
echo -n "" > target.txt

cd 'C:' C:/Users/cmccabe/Desktop/Python27/
echo -n "" > C:/Users/cmccabe/Desktop/Python27/out.txt

# clear Sanger files
cd 'C:' C:/Users/cmccabe/Desktop/Python27/
echo -n "" > C:/Users/cmccabe/Desktop/Python27/*$file.txt




menu() {
    while true
    do
        printf "\n Welcome to target gene annotation, NGS sanger, and mutalyzer (v2), please make a selection from the MENU \n
        ==================================\n\n
        \t 1  GJB2 analysis\n
        \t 2  MECP2 analysis\n
        \t 3  Phox2B analysis\n
		\t 4  NGS sanger analysis\n
        \t 5  Syntax checker\n
        \t 6  Name checker\n
	    \t 7  Position converter\n\n
        ==================================\n\n"

        printf "\t Your choice: "; read menu_choice

        case "$menu_choice" in
        1) gjb2 ;;
        2) mecp2 ;;
        3) phox2b ;;
		4) ngs ;;
        5) syntax ;;
		6) name ;;
		7) position ;;
        *) printf "\n Invalid choice."; sleep 2 ;;
        esac
    done
}

 gjb2() {
    printf "\n\n"
    printf "What is the id of the patient getting GJB2 analysis : "; read id
	printf "Please enter the coding variant the following is an example"
	echo " c.274G>T"
	
    printf "variant(s), use a comma between multiple: "; IFS="," read -a variant
	        
        [ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && return
        [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && return

        for ((i=0; i<${#variant[@]}; i++))
              do printf "NM_004004.5:%s\n" ${variant[$i]} >> c:/Users/cmccabe/Desktop/Python27/out.txt
        done
	add2text ${id}.txt
	additional
}

mecp2() {
    printf "\n\n"
    printf "What is the id of the patient getting MECP2 analysis  : "; read id
	printf "Please enter the coding variant the following is an example"
	echo " c.274G>T"
	
    printf "variant(s), use a comma between multiple: "; IFS="," read -a variant
	
        [ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && return
        [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && return

        for ((i=0; i<${#variant[@]}; i++))
              do printf "NM_004992.3:%s\n" ${variant[$i]} >> c:/Users/cmccabe/Desktop/Python27/out.txt
        done
	add2text ${id}.txt
	additional
}

phox2b() {
    printf "\n\n"
    printf "What is the id of the patient getting Phox2B analysis  : "; read id
	printf "Please enter the coding variant the following is an example"
	echo " c.274G>T"
	
    printf "variant(s), use a comma between multiple: "; IFS="," read -a variant
        
        [ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && return
        [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && return

        for ((i=0; i<${#variant[@]}; i++))
              do printf "NM_003924.3:%s\n" ${variant[$i]} >> c:/Users/cmccabe/Desktop/Python27/out.txt
        done
	add2text ${id}.txt
	additional
}

additional() {
    printf "\n\n"
    printf "Are there additonal target gene patients to be analyzed?  Y/N "; read match_choice

    case "$match_choice" in
        [yY]) id="${id}"; menu ;;
        [nN]) id="${id}"; nomenclature ;;  
    esac
}

ngs() {
    cmd /C "start c:\\Users\\cmccabe\\Desktop\\Sanger\\NGS_Sanger.xlsm"
    cd 'C:' C:/Users/cmccabe/Desktop/Python27/
    for file in C:/Users/cmccabe/Desktop/Python27/*_sanger.txt
  do
    cat $file >> c:/Users/cmccabe/Desktop/Python27/out.txt
  done
  convert
}
	
syntax() {
    printf "\n\n"

	# clear old entries
	cd 'C:' C:/Users/cmccabe/Desktop/Python27/
    echo -n "" > C:/Users/cmccabe/Desktop/Python27/syntax.txt
		
    printf "Please enter HGVS description of variant(s), use a comma between multiple: "; IFS="," read -a hgvs
	
	[ -z "hgvs" ] && printf "\n Nothing entered. Leaving match function." && sleep 2 && return
	
        for ((i=0; i<${#hgvs[@]}; i++))
              do printf ${hgvs[$i]} >> c:/Users/cmccabe/Desktop/Python27/syntax.txt
        done 
		
	# run python SyntaxChecker
    cd 'C:'
    C:/Users/cmccabe/Desktop/Python27/python.exe C:/Users/cmccabe/Desktop/Python27/run_batch_job.py C:/Users/cmccabe/Desktop/Python27/syntax.txt C:/Users/cmccabe/Desktop/Python27/syntax_verify.txt SyntaxChecker
	awk 'NR>1' c:/Users/cmccabe/Desktop/Python27/syntax_verify.txt
	printf "Is this correct?  Y/N "; read match_choice

    case "$match_choice" in
        [yY]) id=""; other ;;
        [nN]) rm c:/Users/cmccabe/Desktop/Python27/syntax_verify.txt; menu ;; 
    esac
}
		
name() {
	printf "\n\n"
	printf "Please enter the gene name: "; read gene
	
	shopt -s nocasematch
        case $gene in
	GJB2)
		code="NM_004004.5"
	;;
	MECP2)
		code="NM_004992.3"
	;;
	Phox2B)
		code="NM_003924.3"
	;;
	*) # everything else we do not won't to process.
		printf "%s \n" "This is not a target gene, or is a intronic variant, Please use one of the following formats to enter variant"
     	echo "
NM_003002.3:c.274G>T (reference transcript:coding change w/ nucleotide change)
chr11:g.111959693G>T (chromosome:genomic position w/ nucleotide change)
NC_000011.9:g.111959693G>T (Genebank accession #:genomic position w/ nucleotide change)"
	;;
esac
printf "%s \n" "Please enter variant(s), use a comma between multiple: "
OLDIFS=$IFS
IFS=","
read -a variants
for (( i = 0; i < ${#variants[@]}; i++ ))
	do
	printf "%s %s\n" "$code" "${variants[$i]}" >> c:/Users/cmccabe/Desktop/Python27/out.txt
	done
IFS=$OLDIFS

    # run python NameChecker
    printf "\n\n"
	cd 'C:'
    C:/Users/cmccabe/Desktop/Python27/python.exe C:/Users/cmccabe/Desktop/Python27/run_batch_job.py C:/Users/cmccabe/Desktop/Python27/out.txt C:/Users/cmccabe/Desktop/Python27/out_name.txt NameChecker
	
    # confirm name and additional?
   awk 'NR>1 { if ($2 ~ /^\(/ ) {$1=""; print "Found error: ", $0} else { sub(/.*:/, "", $1); sub(/.*:/, "", $7); print "No error: " $1 "," $7}}' C:/Users/cmccabe/Desktop/Python27/out_name.txt
	printf "Is the variant correct?  Y/N "; read match_choice
	
    case "$match_choice" in
        [yY]) id=""; other ;; 
        [nN]) cd 'C:' C:/Users/cmccabe/Desktop/Python27/; awk '/variantchecker/ {r[$1]} FNR==NR {next} !($1 in r)' C:/Users/cmccabe/Desktop/Python27/out.txt C:/Users/cmccabe/Desktop/Python27/out.txt > C:/Users/cmccabe/Desktop/Python27/out.txt; annotation ;
    esac
}

position() {                 
    printf "\n\n"
	printf "%s \n" "Please use one of the following formats to enter variant"
     	echo "
NM_003002.3:c.274G>T (reference transcript:coding change w/ nucleotide change)
chr11:g.111959693G>T (chromosome:genomic position w/ nucleotide change)
NC_000011.9:g.111959693G>T (Genebank accession #:genomic position w/ nucleotide change)"

printf "%s \n" "Please enter variant(s), use a comma between multiple: "
OLDIFS=$IFS
IFS=","
read -a variants
for (( i = 0; i < ${#variants[@]}; i++ ))
	do
	printf "%s %s\n" "$code" "${variants[$i]}" >> c:/Users/cmccabe/Desktop/Python27/out.txt
	done
IFS=$OLDIFS

    # run python NameChecker
    printf "\n\n"
	cd 'C:'
    C:/Users/cmccabe/Desktop/Python27/python.exe C:/Users/cmccabe/Desktop/Python27/run_batch_job.py C:/Users/cmccabe/Desktop/Python27/out.txt C:/Users/cmccabe/Desktop/Python27/out_position.txt PositionConverter
	
    # parse output
	  perl -ne 'next if $. == 1;
	if(/.*del([A-Z]+)ins([A-Z]+).*NC_0{4}([0-9]+).*g\.([0-9]+)_([0-9]+)/)   # indel
{
        print join("\t", $3, $4, $5, $1, $2), "\n";
}
else
{
            while (/\t*NC_(\d+)\.\S+g\.(\d+)(\S+)/g) {                                            # conditional parse
                ($num1, $num2, $common) = ($1, $2, $3);
                $num3 = $num2;
                if    ($common =~ /^([A-Z])>([A-Z])$/)   { ($ch1, $ch2) = ($1, $2) }              # SNP
                elsif ($common =~ /^del([A-Z])$/)        { ($ch1, $ch2) = ($1, "-") }             # deletion
                elsif ($common =~ /^ins([A-Z])$/)        { ($ch1, $ch2) = ("-", $1) }             # insertion
                elsif ($common =~ /^_(\d+)del([A-Z]+)$/) { ($num3, $ch1, $ch2) = ($1, $2, "-") }  # multi deletion
				elsif ($common =~ /^_(\d+)ins([A-Z]+)$/) { ($num3, $ch1, $ch2) = ("-", $1, $2) }  # multi insertion
                printf ("%d\t%d\t%d\t%s\t%s\n", $num1, $num2, $num3, $ch1, $ch2);                 # output
                map {undef} ($num1, $num2, $num3, $common, $ch1, $ch2);
            }
}
           ' C:/Users/cmccabe/Desktop/Python27/out_position.txt > C:/Users/cmccabe/Desktop/Python27/out_parse.txt
		   
		
    # dislay cordinates
      printf "The genomic coordinates are: "
	  cat C:/Users/cmccabe/Desktop/Python27/out_parse.txt
	  
	# additional?
	printf "Are there additional things to do?  Y/N "; read match_choice

    case "$match_choice" in
        [yY]) rm echo -n "" > C:/Users/cmccabe/Desktop/Python27/out.txt; rm C:/Users/cmccabe/Desktop/Python27/out_parse.txt; rm C:/Users/cmccabe/Desktop/Python27/out_position.txt; menu ;;
        [nN]) echo -n "" > C:/Users/cmccabe/Desktop/Python27/out.txt; rm C:/Users/cmccabe/Desktop/Python27/out_parse.txt; rm C:/Users/cmccabe/Desktop/Python27/out_position.txt; printf "\n Goodbye! "; sleep 2 && exit ;; 
    esac
}

nomenclature() {                 
 # run python NameChecker
    printf "\n\n"
	cd 'C:'
    C:/Users/cmccabe/Desktop/Python27/python.exe C:/Users/cmccabe/Desktop/Python27/run_batch_job.py C:/Users/cmccabe/Desktop/Python27/out.txt C:/Users/cmccabe/Desktop/Python27/out_name.txt NameChecker
   check
}

other() {
    printf "\n\n"
    printf "Are there other things to do?  Y/N "; read match_choice

    case "$match_choice" in
        [yY]) id=""; menu ;;
        [nN]) id="$"; printf "\n Goodbye! "; sleep 2 && exit ;;  
    esac
}
	
check() {
    printf "\n\n"
	awk 'NR>1 { if ($2 ~ /^\(/ ) {$1=""; print "Found error: ", $0} else { sub(/.*:/, "", $1); sub(/.*:/, "", $7); print "No error: " $1 "," $7}}' C:/Users/cmccabe/Desktop/Python27/out_name.txt
	printf "Is the variant correct?  Y/N "; read match_choice
	
    case "$match_choice" in
        [yY]) id="${id}"; convert ;; 
        [nN]) cd 'C:' C:/Users/cmccabe/Desktop/Python27/; awk '/variantchecker/ {r[$1]} FNR==NR {next} !($1 in r)' c:/Users/cmccabe/Desktop/Python27/out_name.txt c:/Users/cmccabe/Desktop/Python27/out.txt > c:/Users/cmccabe/Desktop/Python27/out.txt; rm c:/Users/cmccabe/Desktop/Python27/out_name.txt; menu ;;
    esac
}

convert() {                 
 # run python PositionConverter
    printf "\n\n"
	cd 'C:'
    C:/Users/cmccabe/Desktop/Python27/python.exe C:/Users/cmccabe/Desktop/Python27/run_batch_job.py C:/Users/cmccabe/Desktop/Python27/out.txt C:/Users/cmccabe/Desktop/annovar/out_position.txt PositionConverter
    parse
}

parse() {
    printf "\n\n"
	cd 'C:\Users\cmccabe\Desktop\annovar'
    perl -ne 'next if $. == 1;
	if(/.*del([A-Z]+)ins([A-Z]+).*NC_0{4}([0-9]+).*g\.([0-9]+)_([0-9]+)/)   # indel
{
        print join("\t", $3, $4, $5, $1, $2), "\n";
}
else
{
            while (/\t*NC_(\d+)\.\S+g\.(\d+)(\S+)/g) {                                            # conditional parse
                ($num1, $num2, $common) = ($1, $2, $3);
                $num3 = $num2;
                if    ($common =~ /^([A-Z])>([A-Z])$/)   { ($ch1, $ch2) = ($1, $2) }              # SNP
                elsif ($common =~ /^del([A-Z])$/)        { ($ch1, $ch2) = ($1, "-") }             # deletion
                elsif ($common =~ /^ins([A-Z])$/)        { ($ch1, $ch2) = ("-", $1) }             # insertion
                elsif ($common =~ /^_(\d+)del([A-Z]+)$/) { ($num3, $ch1, $ch2) = ($1, $2, "-") }  # multi deletion
				elsif ($common =~ /^_(\d+)ins([A-Z]+)$/) { ($num3, $ch1, $ch2) = ("-", $1, $2) }  # multi insertion
                printf ("%d\t%d\t%d\t%s\t%s\n", $num1, $num2, $num3, $ch1, $ch2);                 # output
                map {undef} ($num1, $num2, $num3, $common, $ch1, $ch2);
            }
}
           ' C:/Users/cmccabe/Desktop/annovar/out_position.txt > C:/Users/cmccabe/Desktop/annovar/out_parse.txt
		   annovar
}

annovar() {
    # combine id and position files
	cd 'C:\Users\cmccabe\Desktop\annovar'
	cp out_parse.txt "$(cat target.txt)"
	
	# run annotation
              $( perl -ne 'chomp; system ("perl table_annovar.pl $_ humandb/ -buildver hg19 -protocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -operation g,f,f,f,f,f ")' < target.txt )
			  
     printf "The annotation is complete, would you like analyze additional target gene patients? Y/N "; read match_choice
	 case "$match_choice" in
        [yY]) id="${id}"; menu ;;
        [nN]) id="${id}"; remove ;;
		esac
}

add2text() {
  # $1 is the first argument to add2text
  cd 'C:\Users\cmccabe\Desktop\annovar'
  echo "$1" >> target.txt
}

remove() {
printf "\n\n"
printf "Removing old files, please wait ";
	
  # delete annovar files
  rm 'C:\Users\cmccabe\Desktop\annovar\out_position.txt'
  rm 'C:\Users\cmccabe\Desktop\annovar\out_parse.txt'
  cd 'C:\Users\cmccabe\Desktop\annovar'
  rm "${id}".txt
  rm "${id}".txt.refGene.log
  rm "${id}".txt.hg19_clinvar_dropped
  rm "${id}".txt.hg19_clinvar_filtered
  rm "${id}".txt.hg19_clinvarreference_dropped
  rm "${id}".txt.hg19_clinvarreference_filtered  
  rm "${id}".txt.hg19_clinvarsubmit_dropped  
  rm "${id}".txt.hg19_clinvarsubmit_filtered  
  rm "${id}".txt.hg19_common_dropped  
  rm "${id}".txt.hg19_common_filtered  
  rm "${id}".txt.hg19_popfreq_all_dropped  
  rm "${id}".txt.hg19_popfreq_all_filtered  
  rm "${id}".txt.refGene.variant_function
  rm "${id}".txt.refGene.exonic_variant_function
  rm "${id}".txt.log
  
  # delete python files
  cd 'C:'
  rm  c:/Users/cmccabe/Desktop/Python27/out_name.txt  
  

printf "\n Old files removed, formatting for matrix, please wait ";
	matrix
}

matrix() {
	 cd 'C:\Users\cmccabe\Desktop\annovar'
   	 perl matrix.pl "${id}".txt.hg19_multianno.txt L:/NGS/3_BUSINESS/Matrix/Torrent/matrix_"${id}".txt
	 
	 	 printf "Process complete and new file saved in L:/NGS/3_BUSINESS/Matrix/Torrent/matrix_${id}.txt, Are there additional target gene patients? Y/N "; read match_choice
		 
	 case "$match_choice" in
	    [yY]) id="${id}"; rm C:/Users/cmccabe/Desktop/annovar/"${id}".txt.hg19_multianno.txt; menu ;;
        [nN]) id="${id}"; rm C:/Users/cmccabe/Desktop/annovar/"${id}".txt.hg19_multianno.txt; printf "\n Goodbye! "; sleep 2 && exit ;;
		esac
}

# actual start of this program
menu # run menu function

# 2  
Old 10-28-2015
Why don't you redirect stderr (and maybe stdout) to a file after setting the -x option?
# 3  
Old 10-28-2015
So after setting set -x redirect stderr of what? I'm not sure what you mean, sorry but thank you Smilie.
# 4  
Old 10-28-2015
Code:
set -x
exec 2> errors.txt

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 10-28-2015
Quote:
Originally Posted by cmccabe
So after setting set -x redirect stderr of what? ....
stderr of the entire script.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 10-28-2015
I tried the below and no errors.txt results but the bash does print something and close quickly. Thank you Smilie.

Code:
 #!/bin/bash
set -x
exec 2> C:\Users\cmccabe\Desktop\bash\errors.txt

# 7  
Old 10-28-2015
Backslashes are special to BASH, remember. You probably need to double them up before this will work.

Code:
exec 2> C:\\Users\\cmccabe\\Desktop\\bash\\errors.txt

"work" just means "create that file for you to read later", not "make the errors not have happened and work perfectly".
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Menu using zenity

Hi, I'm new to bash, and have an example menu script using Zenity. It works fine if the user enters A B or C, but if the user enters nothing, I can only figure out how to exit the script. How do I get the menu to reappear if no input is selected? The script is: title="Select example"... (2 Replies)
Discussion started by: allen11
2 Replies

2. Shell Programming and Scripting

Bash script jumping too quickly to the next command - crash

Hi, (I am not too good in command-lines). I am running a bash script and at some point my loop starts to run super quickly. Then, the steps after do not get processed and there is no further outpout generated. I tried with the sleep 20s option, as I thought some steps would get overlapped, but... (22 Replies)
Discussion started by: tremblayemilie9
22 Replies

3. Shell Programming and Scripting

Bash menu opens and closes

Ever since I added these two code blocks to my bash menu it just opens and closes right away. I use a batch file that worked fine until these codes were added and I am not sure what is wrong. Basically, what I am trying to do in the additional section is if the answer is "Y" then it goes back... (13 Replies)
Discussion started by: cmccabe
13 Replies

4. Shell Programming and Scripting

get chosen value from bash menu

Hi again :) This is just a sample whiptail menu. Works great, but have been trying to get the chosen value into a variable but failing pretty bad...its ther but unsure how to echo it out when needed #! /bin/bash #This is the menu whiptail --title "Menu example" --menu "Choose an... (9 Replies)
Discussion started by: olearydc
9 Replies

5. Shell Programming and Scripting

Help regarding a bash menu script

Greetings all, I'm having some trouble writing a menu drive bash script, actually coding the menu part was not difficult however its a problem with a menu option I'm having trouble with. My menu has 5 options, when the user selects the second option, they are then prompted to enter a number from... (5 Replies)
Discussion started by: Vitrophyre
5 Replies

6. Shell Programming and Scripting

Bash menu script

I have a main menu quit=n while do clear echo echo "1. General system information" echo "2. Hardware utilisation information" echo "3. File management" echo "4. User information" echo "5. Information on network connectivity" echo "6. Information on processes" echo "Q.Quit" ... (3 Replies)
Discussion started by: AngelFlesh
3 Replies

7. UNIX for Dummies Questions & Answers

need solution for this quickly. please quickly.

Write a nawk script that will produce the following report: ***FIRST QUARTERLY REPORT*** ***CAMPAIGN 2004 CONTRIBUTIONS*** ------------------------------------------------------------------------- NAME PHONE Jan | ... (5 Replies)
Discussion started by: p.palakj.shah
5 Replies

8. UNIX for Dummies Questions & Answers

Major OS errors/Bash errors help!!!!

Hi all, dummy here.... I have major errors on entering the shell. On login I get: -bash: dircolors: command not found -bash: tr: command not found -bash: fgrep: command not found -bash: grep: command not found -bash: grep: command not found -bash: id: command not found -bash: [: =: unary... (12 Replies)
Discussion started by: wcmmlynn
12 Replies

9. Programming

multiuser chat server closes when one client closes. code included

I have been trying to write a very basic chat program but at the moment I am having problems getting it to be multiuser as it closes all connections when one client shutsdown. I have also been having problems trying to get the program to display a list of usernames to the clients. I have tried... (0 Replies)
Discussion started by: dooker
0 Replies
Login or Register to Ask a Question