Change directory error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change directory error
# 1  
Old 11-10-2014
Change directory error

bash ~/match.sh runs fine.
Code:
#!/bin/bash
	printf "Enter ID  : "; read id
	printf "What panel: "; read panel
		cd 'C:\Users\cmccabe\Desktop\annovar'
			[ -z "$id" ] && break
			[ "$id" = "end" ] && break
		OMR=Output_Mutation_Report
			perl -aF/\\t/ -lne 'BEGIN{%m=map{chomp;s/\cM|\cJ//g;$p=join("\t",(split/\t/)[4,5]);($p,$_)} <>;$m{"#CHROM\tINFO"}=$m{"Chr\tSegment Position"}};/SEGPOS=(\d+)/ || /\t(INFO)\t/ or next;$p=$F[0]."\t".$1;exists $m{$p} and print join("\t",$_,$m{$p})' ${id}_${panel}_${OMR}.txt < ${id}_${panel}_${OMR}_Filtered.vcf > ${id}_matched.vcf

I am trying to combine shells: bash ~/newbatch.sh
Code:
#!/bin/bash
	    printf "What is the id of the patient to be matched  : "; read id
	    printf "What panel: "; read panel
		case "$id" in
		cd 'C:\Users\cmccabe\Desktop\annovar'
	    	    OMR=Output_Mutation_Report
			$( perl -aF/\\t/ -lne 'BEGIN{%m=map{chomp;s/\cM|\cJ//g;$p=join("\t",(split/\t/)[4,5]);($p,$_)} <>;$m{"#CHROM\tINFO"}=$m{"Chr\tSegment Position"}};/SEGPOS=(\d+)/ || /\t(INFO)\t/ or next;$p=$F[0]."\t".$1;exists $m{$p} and print join("\t",$_,$m{$p})' ${id}_${panel}_${OMR}.txt < ${id}_${panel}_${OMR}_Filtered.vcf > ${id}_matched.vcf )
		;;
show_menu=true
	menu=( [yY] [nN] )
	func_show() { # "MESSAGE STRING"
	# Prints passed argument
	# Returns nothing
		printf '\n\t%s\n\n' "$1"
	}
#
#	Display & Action
#
	clear
	while $show_menu
	do	printf '#----------------------#\n%s\n' "Are there additonal patients to be matched"
		select entry in "${menu[@]}" Back
		do	case "$entry" in
			Back)		show_menu=false
					;;
			[yY])	$( perl -aF/\\t/ -lne 'BEGIN{%m=map{chomp;s/\cM|\cJ//g;$p=join("\t",(split/\t/)[4,5]);($p,$_)} <>;$m{"#CHROM\tINFO"}=$m{"Chr\tSegment Position"}};/SEGPOS=(\d+)/ || /\t(INFO)\t/ or next;$p=$F[0]."\t".$1;exists $m{$p} and print join("\t",$_,$m{$p})' ${id}_${panel}_${OMR}.txt < ${id}_${panel}_${OMR}_Filtered.vcf > ${id}_matched.vcf ) (index id 0) :: $entry
					;;
			[nN])	func_show "printf "Does the file need to be converted  : " ; read id :: ${menu[${#menu[@]}-1]}"
			while true
do
        printf "Enter ID  : " ; read id
		cd 'C:\Users\cmccabe\Desktop\annovar'
        [ -z "$id" ] && break
        [ "$id" = "end" ] && break
        $( perl convert2annovar.pl -includeinfo -format vcf4old ${id}_matched.vcf > ${id}_matched.avinput )
done
					;;
			*)		
			esac
			break
		done
	done

I am sure the code needs work, but why does it throw an error in the underlined part when it was fine before? Thanks Smilie.

Code:
 bash ~/newbatch.sh
What is the id of the patient to be matched  : H62947
What panel: Epilepsy70
/home/cmccabe/newbatch.sh: line 5: syntax error near unexpected token `'C:\Users\cmccabe\Desktop\annovar''
/home/cmccabe/newbatch.sh: line 5: ` cd 'C:\Users\cmccabe\Desktop\annovar''

# 2  
Old 11-10-2014
It wasn't fine before .. you changed it.

2nd version added this:

Code:
case "$id" in

turning the underlined portion into part of a case statement - and it's the wrong syntax for a case statement.

What are you trying to do?
# 3  
Old 11-10-2014
How about reading through all the related threads of yours to this very same topic/script!
The way you act, you deny any (already) given answer to you, to anyone who is reading the current thread.
Please show some effort and actualy change what you already have been told to, as in, adapt/apply some of the input you were given already.

Cheers

Last edited by sea; 11-10-2014 at 02:35 PM..
This User Gave Thanks to sea For This Post:
# 4  
Old 11-10-2014
Your code is relatively hard to read, I had real difficulties to decipher the program flow.
I wrote a script which utilizes functions for better understanding.

It assumes following program flow and to me it seems to make more sense than
match -> additional -> additional -> convert (repeated manual input of ids)

Code:
menu -> match -> convert? -> NO -> additional? -> NO -> menu
        ^               |          |         |
        |               +--> YES --+         +--> YES +
        |                                             |
        +---------------------------------------------+

Feel free to comment out the "DEBUG INFO" lines if all works as expected; I put them there to see if the variables still contained the values provided in the match function.

Hope this helps.
Code:
#!/bin/bash

menu() {
    clear
    printf "\n MENU \n
    ==================================\n\n
    \t 1  Match patient\n
    \t 2  Exit\n\n
    ==================================\n\n"

    printf "\t Your choice: "; read menu_choice

    case "$menu_choice" in
        1) match ;;
        2) printf "\n Bye! \n\n"; exit ;;
        *) printf "\n Invalid choice."; sleep 2; menu ;;
    esac
}


match() {
    printf "\n\n"
    printf "DEBUG INFO: VALUE OF \$id: %s, VALUE OF \$panel: %s\n" $id $panel
    printf "What is the id of the patient to be matched  : "; read id
    printf "What panel: "; read panel

    [ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && menu
    [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && menu

    cd 'C:\Users\cmccabe\Desktop\annovar'
    OMR=Output_Mutation_Report
    $( perl -aF/\\t/ -lne 'BEGIN{%m=map{chomp;s/\cM|\cJ//g;$p=join("\t",(split/\t/)[4,5]);($p,$_)} <>;$m{"#CHROM\tINFO"}=$m{"Chr\tSegment Position"}};/SEGPOS=(\d+)/ || /\t(INFO)\t/ or next;$p=$F[0]."\t".$1;exists $m{$p} and print join("\t",$_,$m{$p})' ${id}_${panel}_${OMR}.txt < ${id}_${panel}_${OMR}_Filtered.vcf > ${id}_matched.vcf )
    convert
}

convert() {
    printf "\n\n"
    printf "DEBUG INFO: VALUE OF \$id: %s, VALUE OF \$panel: %s\n" $id $panel
    printf "Does the file need to be converted? Y/N "; read convert_choice
    
    case "$convert_choice" in
        [yY]) $( perl convert2annovar.pl -includeinfo -format vcf4old ${id}_matched.vcf > ${id}_matched.avinput )
        additional ;;
        [nN]) additional ;;  
        *) convert ;;
    esac
}

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

    case "$match_choice" in
        [yY]) id=""; panel=""; match ;;
        [nN]) id=""; panel=""; menu ;;  
        *) additional ;;
    esac
}

# actual start of this program
menu # run menu function

# 5  
Old 11-10-2014
Thank you all Smilie... I am a scientist learning awk so I don't mean to disregard any advice, I truly appreciate it.

Junior-Helper your code works great and is much easier to read and follow the flow. How do you do things like:

Code:
 match -> additional -> additional -> convert (repeated manual input of ids)

Code:
menu -> match -> convert? -> NO -> additional? -> NO -> menu
        ^               |          |         |
        |               +--> YES --+         +--> YES +
        |                                             |
        +---------------------------------------------+

and use a collapse function for all things related to convert in line 36 or additional in line 49. I use notepad++ on a windows machine. Thank you all again Smilie.

Last edited by cmccabe; 11-10-2014 at 05:18 PM..
# 6  
Old 11-11-2014
Quote:
Originally Posted by cmccabe
Thank you all Smilie... I am a scientist learning awk so I don't mean to disregard any advice, I truly appreciate it.

Junior-Helper your code works great and is much easier to read and follow the flow. How do you do things like:

Code:
 match -> additional -> additional -> convert (repeated manual input of ids)

Code:
menu -> match -> convert? -> NO -> additional? -> NO -> menu
        ^               |          |         |
        |               +--> YES --+         +--> YES +
        |                                             |
        +---------------------------------------------+

and use a collapse function for all things related to convert in line 36 or additional in line 49. I use notepad++ on a windows machine. Thank you all again Smilie.
I'm a little bit confused. You say you're learning awk, but there is no awk code in this thread.

Drawing ASCII art may be easier in vi than in notepad++, but you shouldn't have any problem even in notepad++ as long as you choose a fixed width font (e.g., courier).

Note that there is one problem with the code that junior-helper provided: None of the functions ever return! Instead of returning, each of the functions recursively calls one of the other functions. Every time the script processes another patient, panel, or bad response, it increases the size of the shell's stack. As the stack grows, the shell will slow down until (if enough patients and panels are entered before the user exits the script), it will run into a stack memory allocation limit and die. The following alternative script with a flowchart more like:
Code:
			    /\		       /\
			   /  \<-----NO-------/  \<---------------+
			  /    \	     /    \	     +----|----+
 +-------+   +------+	 /  new \	    / new  \	     | process |
 | start |-->| menu |-->< patient>--YES--->< panel  >--YES-->} patient ]
 +-------+   +------+	 \   ?  /	    \  ?   /	     | & panel |
			  \    /    +------+ \    /	     +---------+
       +-------------------\  /-NO->| exit |  \  /-------------------+
       |		    \/      +------+   \/		     |
       |		    ^			^		     |
       |		    |			|		     |
       +--Invalid response--+		    	+--Invalid response--+

uses a couple of loops (instead of recursion) to avoid the continually growing stack issue:
Code:
#!/bin/bash

# Main menu...
menu() {
	# Loop reading patient IDs; exit on zero or EOF.
	while [ 1 ]
	do	clear
		printf '\n\nEEnter patient ID (0 to exit): '
		if ! read id || [ "$id" = 0 ]
		then	break
		fi
		if [ "$id" = "" ] || [ "$id" != "${id#*[^0-9]}" ]
		then	printf '\nPatiend ID must be numeric; try again.\n'
			sleep 5
			continue
		fi
		process_panels
	done
        printf "\nBye!\n"
	exit
}

# Proess panels for the current patient ID
process_panels() {
	# Loop reading panels to process for current patient ID ($id)...
	while [ 1 ]
	do	printf "\nEnter panel number for patient ID $id (0 when done): "
		if ! read panel || [ "$panel" = 0 ]
		then	return
		fi
		if [ "$panel" = "" ] || [ "$panel" != "${panel#*[^0-9]}" ]
		then	printf '\nPanel number must be numeric; try again\n'
			continue
		fi

		# Process this panel for this patient...
		printf "DEBUG INFO: Processing patient ID $id, panel $panel\n"
		$(perl -aF/\\t/ -lne 'BEGIN{%m=map{chomp
			s/\cM|\cJ//g
			$p=join("\t",(split/\t/)[4,5])
			($p,$_)} <>
			$m{"#CHROM\tINFO"}=$m{"Chr\tSegment Position"}}
			/SEGPOS=(\d+)/ || /\t(INFO)\t/ or next
			$p=$F[0]."\t".$1
			exists $m{$p} and print join("\t",$_,$m{$p})' \
			${id}_${panel}_${OMR}.txt \
			< ${id}_${panel}_${OMR}_Filtered.vcf \
			> ${id}_matched.vcf)
		printf "\nConvert patient ID $id, panel $panel file (Y/N)?: "
		read convert_choice
		case "$convert_choice" in
		([yY])	printf "Converting patient ID $id, panel $panel file.\n"
			$ perl convert2annovar.pl -includeinfo \
				-format vcf4old ${id}_matched.vcf \
				> ${id}_matched.avinput)
		esac
	done
}

# Initialize location and variables...
cd 'C:\Users\cmccabe\Desktop\annovar'
OMR=Output_Mutation_Report

# Loop through user supplied patient IDs and loop though panels for each
# patient...
menu

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 11-11-2014
cmccabe,
If I understand correctly, the answer to your first question regarding the ASCII flowchart is "I did it manually in gedit (https://wiki.gnome.org/Apps/Gedit) with a monospaced font (https://en.wikipedia.org/wiki/Monospaced_font) conveniently called Monospace".

I don't understand what you mean with "collapse function". Seems to be a notepad++ question Smilie
Sorry, I have neither notepad++ nor a Windows machine.


Don,
thank you for the hint.
Is it possible to make the functions "return", without major changes?
I admit I misused the functions as goto replacement Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change directory shell

#!/bin/bash echo -n "Enter number of sanger patients : "; read id 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 -otherinfo")' < file.txt I have the above script... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Change Directory

Hi All, There is a code like below in my script ############################################### ###Create Directories and Sub-Directories ############################################### dpdir=DP_FROM_${from}_TO_${to} mkdir $dpdir cd $dpdir mkdir AWQM WFCONTROLLER PROVCO PRISM ... (1 Reply)
Discussion started by: pvmanikandan
1 Replies

3. Shell Programming and Scripting

Change to directory and search some file in that directory in single command

I am trying to do the following task : export ENV=aaa export ENV_PATH=$(cd /apps | ls | grep $ENV) However, it's not working. What's the way to change to directory and search some file in that directory in single command Please help. (2 Replies)
Discussion started by: saurau
2 Replies

4. UNIX for Dummies Questions & Answers

How to change database directory to another directory?

Hi, I Installed mysql on my CentOS 6.2 Server. But when I tried to change the location of /var/lib/mysql to another directory. I can't start the mysql. Below is what I've done yum install mysql mysql-server mysql-devel mkdir /path/to/new/ cp -R /var/lib/mysql /path/to/new chown -R... (1 Reply)
Discussion started by: ganitolngyundre
1 Replies

5. Shell Programming and Scripting

change directory if available

I have a simple shell script that prompts the user to enter a directory to navigate to. What i want it to do and i don't know how to do this is if the directory is invalid automatically navigate to the home directory. echo "enter a directory to navigate to:" read directory cd $directory... (6 Replies)
Discussion started by: icelated
6 Replies

6. Shell Programming and Scripting

Change all filenames in a directory

I have a directory of files and each file has a random 5 digit string at the beginning that needs to be removed. Plus, there are some files that will be identically named after the 5 digit string is removed and I want those eliminated or moved. any ideas? (17 Replies)
Discussion started by: crumb
17 Replies

7. UNIX for Dummies Questions & Answers

Change Directory

I have a directory that is existing under my root dir of the FTP server. The DIR name is 'Software Patch'. I want to move in to that DIR to download some patches. But, when I issued a command 'cd SOftware Patch', the system said that it cannot find the dir 'Software'. I tried all possible ways like... (2 Replies)
Discussion started by: vskr72
2 Replies

8. Shell Programming and Scripting

Change Directory via a script?

I would like to have a script that would change my current working directory. However, any time I execute a 'cd' command in a script, it holds only for the life of that script -- the working directory on exit is the same as when the script was initiated. Is it possible to have the script return... (3 Replies)
Discussion started by: George Borrmann
3 Replies

9. Shell Programming and Scripting

change directory

hi, Iam in directory A. I run a script from there. inside the script i have a command cd B. When i come out of the script directory is A only. Even when i come out scrip i want the directory to be B How to achieve (2 Replies)
Discussion started by: mkan
2 Replies

10. Shell Programming and Scripting

change directory

Hi all, I'm trying to wirte a small shell script in Linux. My script has the flow like, cmd1 cmd2 cd testdata cmd3 After exiting the program, the CWD remains the same as where I execute the program. I need it to be changed to the latest updated directory in the program. How can I do... (1 Reply)
Discussion started by: vadivel
1 Replies
Login or Register to Ask a Question