Code exits before completing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Code exits before completing
# 1  
Old 04-03-2015
Code exits before completing

The lines in bold check the value of a user input, if it is not one of the expected genes (GJB2, MECP2, PHOX2B), the user is shown a list of formats to use and the variant is entered. It is then supposed to printf that entered variant? Thank you Smilie.


Code:
name() {
	printf "\n\n"
	printf "Please enter the gene name  : "; read gene
	
	[ -z "$gene" ] &&
		printf "\n No ID supplied. Leaving match function." &&
		sleep 2 &&
		return
	[ "$gene" = "end" ] &&
		printf "\n Leaving match function." &&
		sleep 2 &&
		exit
	[ "$gene" != "GJB2 or MECP2 or PHOX2B" ] &&
		printf "\n That is not a target gene please use one of the formats listed." &&
		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 "Enter variant(s): "; IFS="," read -a variants

	code=""
	if [ "$gene" = "GJB2" ]
	then	code="NM_004004.5"
	elif [ "$gene" = "MECP2" ]
	then	code="NM_004992.3"
	elif [ "$gene" = "Phox2B" ]
	then	code="NM_003924.3"
	else	return
	fi
	for ((i = 0; i < ${#variants[@]}; i++))
	do	printf "%s:%s\n" "$code" "${variants[$i]}"
	done | tee -a c:/Users/cmccabe/Desktop/Python27/$gene.txt \
			>> c:/Users/cmccabe/Desktop/Python27/out.txt
}

added code here

Will a 3rd elif added to print this input be ok (at the end) ?

Code:
 
code=""
	if [ "$gene" != "GJB2 or MECP2 or PHOX2B" ]
	then	code=printif ((i = 0; i < ${#variants[@]}; i++))
	                tee -a c:/Users/cmccabe/Desktop/Python27/$gene.txt \
			         >> c:/Users/cmccabe/Desktop/Python27/out.txt


Last edited by cmccabe; 04-03-2015 at 06:38 PM..
# 2  
Old 04-04-2015
You should try to be consistent when using printf, use always same format.
Return IFS to old value, it is good practice so you don't get surprises in code later on.

As the volume of elifs increases the code will be harder to maintain with a higher margin for error.

Use case perhaps ?

Code:
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 ..."
	;;
esac
printf "%s \n" "Enter variant(s): "
OLDIFS=$IFS
IFS=","
read -a variants
for (( i = 0; i < ${#variants[@]}; i++ ))
	do
	printf "%s %s\n" "$code" "${variants[$i]}" # is plain redirection ok here instead of using tee ?
	done
IFS=$OLDIFS

If users are providing input to your scripts, i would recommend using getopts and passing those via command line e.g ./myscript -a $gene -b variant1,variant2

Reason is simple, if you ever decide to automate the input (predefined input for all variables) it will be simple, opposing to using approach you are using now.

Hope that helps
Regards
Peasant.
This User Gave Thanks to Peasant For This Post:
# 3  
Old 04-04-2015
Perfect, that works great... Thank you Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Congratulations RudiC for completing 4000+ THANKS in forums.

Hello All forum members, I would like to take this opportunity to THANK RudiC for his tremendous achievement, guidance, help for helping in forums, let us join our hands together for his GREAT achievement :b: @Rudi sir, How are you sir? you ROCK, please keep up the great work sir :b: ... (1 Reply)
Discussion started by: RavinderSingh13
1 Replies

2. Shell Programming and Scripting

Bash not calling function after completing

In the below bash the function execute is started with the while true at the end of the script. Currently, this works and processing does begin. The function panel in bold does not get called after execute completes, therefore the user does not get the menu selection and the files just loop... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Need help with completing a bash script

Hello All, I am automating a task using bash script and got stuck at this. From the below input, Meta Device Members (20) : { ---------------------------------------------------------------------- BCV DATA RDF DATA ... (7 Replies)
Discussion started by: Sam R
7 Replies

4. Shell Programming and Scripting

Looping not completing in shell script

Hi, Iam using below code to login to servers to get cpu utilisation. but output is coming for only one server. code is below root@blr-svr-oclan-01 # more SSSC_CPU_UTIL1.sh #!/bin/sh echo "CPU UTILIZATION" while read line; do IDLE=`/usr/local/bin/sshpass -p 'xxx' ssh xxx@$line 'sar 2 2' |... (1 Reply)
Discussion started by: surender reddy
1 Replies

5. Homework & Coursework Questions

Help completing lab.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: ''you are missing the /home/smichaels/Labs/lab2b/group file, please create it as per step 12 of the lab. once... (4 Replies)
Discussion started by: ink
4 Replies

6. Shell Programming and Scripting

shell script exiting before completing

I have a script which has the following statement, /opt/oracle/product/9i/bin/sqlplus << EOC >> $LOG_FILE 2>&1 username/password ---- Enters the SQL prompt @/export/home/oracle/shells/grant_userview.sql ---Runs the SQL script @/export/home/oracle/shells/grant_proc_userview.sql ---Runs the... (6 Replies)
Discussion started by: welldone
6 Replies

7. UNIX for Dummies Questions & Answers

Completing Command Entry without Typing

Hi, In my UNIX (KSH) sytem, in order to Complete Command Entry without Typing the whole path or name, I have to use ESC+\ where as I am used to press TAB key to do the same. Can anyone tell me where we need to change the settings of the keyboard combination so that when I press TAB key the file... (6 Replies)
Discussion started by: jisha
6 Replies

8. Shell Programming and Scripting

tcsh + completing a sub-directory entry

As part of a script Im designing, I am required to allow the command line to finish off part of a series of sub-directory entries. Basically what I want to do is cp /root/parent/part of subs name + other part of subs name/file /other_root/other_parent/other_sub/file without facing... (0 Replies)
Discussion started by: JamesGoh
0 Replies

9. UNIX for Dummies Questions & Answers

Completing ffmpeg installation

Hello. i am new to unix, though have quite a substantial background of other systems. i recently installed ffmpeg and mencoder on a unix server, which holds a website, in order to use these programs from the website. after installation, i can activate both commands directly using putty, no... (1 Reply)
Discussion started by: noamon
1 Replies

10. Shell Programming and Scripting

Terminate session on completing script

Hai all.. How do i terminate my telnet session automatically when my java applicatiion exits. i have a file run which executes my java application and takes care of all class and library path settings prior to the execution. I would like to terminate my session when my application exits. The... (4 Replies)
Discussion started by: deepsteptom
4 Replies
Login or Register to Ask a Question