Bash to goto specific line/function and start processing if user response is yes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash to goto specific line/function and start processing if user response is yes
# 1  
Old 05-24-2017
Bash to goto specific line/function and start processing if user response is yes

In the bash below I am trying to run the script entire script including the ....(which is a bunch of code) and then in the run function if the user response is y (line in bold). then start processing from execute function. Basically, goto the # extract folder for variable filename line and start processing. I know bash does not have a goto, so I thought a function or is there a better way? Thank you Smilie.

Currently the script does seem to execute but it completes in under minute with no new folders and files created. If I remove the execute() {}, the script functions properly, producing all folders and files, taking ~3 hours to complete.

Code:
...
...
...
...
 execute() {
# extract folder for variable filename
filename=$(awk 'ENDFILE {line=$0} FNR<NR && line ~ $1' /home/cmccabe/medex.logs/folder.log /home/cmccabe/medex.logs/analysis.log)
 run() {
    printf "\n\n"
    printf "All folders have been analyzed, are ready for import, and archieved, are there runs to be analyzed?  Y/N "; read match_choice
     case "$match_choice" in
        [yY]) execute; break;;
       [nN]) remove; printf "Goodbye! "; sleep 2 && exit;;
    esac
}
 backup() {
# move folder to backup
mv -v /home/cmccabe/Desktop/NGS/API/$filename /media/cmccabe/"My Book Western Digital"/ClinicalRuns; run; break;;
}
 identifier() {
# associate name with 20x low coverage file
            cd /home/cmccabe/Desktop/NGS/API/$filename/lowcoverage/20x
            /home/cmccabe/Desktop/NGS/scripts/associate.sh $file
# associate name with 20x gene percent file
            cd /home/cmccabe/Desktop/NGS/API/$filename/panel/20x/percent
            /home/cmccabe/Desktop/NGS/scripts/associate.sh $file
# associate name with vcf file
            cd /home/cmccabe/Desktop/NGS/API/$filename/vcf/panel/annovar
            /home/cmccabe/Desktop/NGS/scripts/associate.sh $file
}
 move() {
# move specific folders to MedexPatients on medex CIFS share
mkdir -p /home/cmccabe/medex/MedexPatients/$filename
rsync -av --exclude='/home/cmccabe/Desktop/NGS/API/$filename/bedtools' 
 # move to GenePerecent on CIFS share medex
rsync -av /home/cmccabe/Desktop/NGS/API/$filename/panel/20x/percent/*.txt /home/cmccabe/medex/GenePercent
 # move to Quality Metrics
rsync -av /home/cmccabe/Desktop/NGS/API/$filename/pdf/*.txt /home/cmccabe/medex/QualityMetrics
}
 additional() {
    printf "\n\n"
    printf "Are there additonal files to be analyzed?  Y/N "; read match_choice
     case "$match_choice" in
        [yY]) menu; break;;
        [nN]) identifier; move; printf "Select folders and files from "/home/cmccabe/Desktop/NGS/API/$filename" have been moved to MedexPatients on medex as well as to the import folders, the folder will now be achieved "; backup; break;;
    esac
}
 while true; do
    read -p "Do you want to get coverage of a specific gene, genes, or panel?" yn
    case $yn in
        [Yy]* ) menu; break;;
        [Nn]* ) move; break;;
        * ) echo "Please answer yes or no.";;
    esac
done
}


Last edited by cmccabe; 05-24-2017 at 02:04 PM.. Reason: fixed typo
# 2  
Old 05-24-2017
You cannot nest function declarations, the contents of run() belong outside the contents of execute().
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-24-2017
Throws an unexpected ;; on the mv line in # move folder to backup. Removing the ;; does allow the scrip[t to execute but with the same results (processes too quickly). Did I misunderstand or do something else? Thank you very much Smilie.

Code:
 ...
...
...
...
run() {
    printf "\n\n"
    printf "All folders have been analyzed, are ready for import, and archieved, are there runs to be analyzed?  Y/N "; read match_choice
     case "$match_choice" in
        [yY]) execute; break;;
       [nN]) remove; printf "Goodbye! "; sleep 2 && exit;;
    esac
}
  execute() {
# extract folder for variable filename
filename=$(awk 'ENDFILE {line=$0} FNR<NR && line ~ $1' /home/cmccabe/medex.logs/folder.log /home/cmccabe/medex.logs/analysis.log)
 
 backup() {
# move folder to backup
mv -v /home/cmccabe/Desktop/NGS/API/$filename /media/cmccabe/"My Book Western Digital"/ClinicalRuns; run; break;;
}
 identifier() {
# associate name with 20x low coverage file
            cd /home/cmccabe/Desktop/NGS/API/$filename/lowcoverage/20x
            /home/cmccabe/Desktop/NGS/scripts/associate.sh $file
# associate name with 20x gene percent file
            cd /home/cmccabe/Desktop/NGS/API/$filename/panel/20x/percent
            /home/cmccabe/Desktop/NGS/scripts/associate.sh $file
# associate name with vcf file
            cd /home/cmccabe/Desktop/NGS/API/$filename/vcf/panel/annovar
            /home/cmccabe/Desktop/NGS/scripts/associate.sh $file
}
 move() {
# move specific folders to MedexPatients on medex CIFS share
mkdir -p /home/cmccabe/medex/MedexPatients/$filename
rsync -av --exclude='/home/cmccabe/Desktop/NGS/API/$filename/bedtools' 
 # move to GenePerecent on CIFS share medex
rsync -av /home/cmccabe/Desktop/NGS/API/$filename/panel/20x/percent/*.txt /home/cmccabe/medex/GenePercent
 # move to Quality Metrics
rsync -av /home/cmccabe/Desktop/NGS/API/$filename/pdf/*.txt /home/cmccabe/medex/QualityMetrics
}
 additional() {
    printf "\n\n"
    printf "Are there additional files to be analyzed?  Y/N "; read match_choice
     case "$match_choice" in
        [yY]) menu; break;;
        [nN]) identifier; move; printf "Select folders and files from "/home/cmccabe/Desktop/NGS/API/$filename" have been moved to MedexPatients on medex as well as to the import folders, the folder will now be achieved "; backup; break;;
    esac
}
 while true; do
    read -p "Do you want to get coverage of a specific gene, genes, or panel?" yn
    case $yn in
        [Yy]* ) menu; break;;
        [Nn]* ) move; break;;
        * ) echo "Please answer yes or no.";;
    esac
done
}


Last edited by cmccabe; 05-24-2017 at 03:20 PM.. Reason: added details
# 4  
Old 05-24-2017
'break' is meaningless outside a while loop, remove it instead of the ;;

There should also be a space before ;;
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 05-25-2017
I removed the break but there appears to be an issue with the syntax (though I am following the format of previous scripts that work).

Code:
run() {
    printf "\n\n"
    printf "All folders have been analyzed, are ready for import, and archieved, are there runs to be analyzed?  Y/N "; read match_choice

    case "$match_choice" in
        [yY]) execute ;;
        [nN]) remove; printf "Goodbye! "; sleep 2 && exit ;;
    esac
}

backup() {
# move folder to backup
mv -v /home/cmccabe/Desktop/NGS/API/$filename /media/cmccabe/"My Book Western Digital"/ClinicalRuns; run ;;
}

/home/cmccabe/Desktop/NGS/scripts/lch.sh: line 35: syntax error near unexpected token `)'
/home/cmccabe/Desktop/NGS/scripts/lch.sh: line 35: `        [nN]) remove; printf "Goodbye! "; sleep 2 && exit  ;;'

Thank you Smilie.

I added an while loop and re-structured, seems to be working. Thank you Smilie.

Last edited by cmccabe; 05-25-2017 at 12:54 PM.. Reason: added edit
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash function to suppress warning message for specific text and display prompt

In the below bash function multiple variants are input and stored in a variable $variant, and each is written to an out file at c:/Users/cmccabe/Desktop/Python27/out.txt stored on a separate line. # enter variant phox2b() { printf "\n\n" printf "What is the id of the patient getting... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Solaris

Limit bash/sh user's access to a specific directory

Hello Team, I have Solaris 10 u6 I have a user test1 using bash that belong to the group staff. I would like to restrict this user to navigate only in his home directory and his subfolders but not not move out to other directories. How can I do it ? Thanks in advance (1 Reply)
Discussion started by: csierra
1 Replies

3. Shell Programming and Scripting

Looping structure to make up for lack of bash GOTO

Hello, I am re-processing some files when a specific condition is met. The condition is read from the filename. Since files may need to be re-processed a number of times before they no longer meet the condition, I need to know when to stop re-processing. I am having trouble visualizing the... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

4. Shell Programming and Scripting

Bash - Appending to specific line in file

I'm working on a personal project, a multiplication quiz script for my kids. In it, the user's performance will be recorded and written to a file. After they've played it a little while, it will start to focus more on the ones that give them the most trouble-- that take a long time to answer or... (4 Replies)
Discussion started by: treesloth
4 Replies

5. Red Hat

User (Profile) Specific Start Menu for RHEL 6.1 using KDE 4.3.4

I installed RHEL 6.1 with KDE 4.3.4 using LDAP on a PC and have now the problem to specifiy a user/Profile specific start menu. I have used already the kmenuedit tool and tailored for one user a specific menu, successfully. I changed as an example the konsole submenu. The kmenuedit was... (0 Replies)
Discussion started by: Gromit
0 Replies

6. Shell Programming and Scripting

Put a # in start of a specific line of a file

Hello Guys Please let me know how to solve the below issue I have a file like below drop table R1416.ABC1 cascade constraints; drop table R1416.ABC2 cascade constraints; drop table R1416.ABC3 cascade constraints; drop table R1416.ABC4 cascade constraints; drop table R1416.ABC5... (7 Replies)
Discussion started by: Pratik4891
7 Replies

7. UNIX for Advanced & Expert Users

Appending # to the start of specific line in a properties file

Hi, I have the following file, ABC.txt: ABC=123 DEF=234 FGH=345 Based on my validation and conditional processing it is observed that i need to comment or append # before DEF=234 so the same file ABC.txt should look as follows ABC=123 #DEF=234 FGH=345 Sorry if its a... (6 Replies)
Discussion started by: mihirvora16
6 Replies

8. Shell Programming and Scripting

BASH - set specific user variable via string operators

Apologies for the utter triviality of this question, but we all have to start somewhere! I've also tried searching but this question is pretty vague so I didn't (a) really know what to search for or (b) get many relevant hits to what I did search for. Anyway, I'm in the process of self-teaching... (1 Reply)
Discussion started by: u5j84
1 Replies

9. Shell Programming and Scripting

How to read the value from a specific line and column BASH

Hi All, I have the same problem as the one posted in https://www.unix.com/shell-programming-scripting/96097-how-read-value-specific-line-column-csh-variable.html but I'm using bash. Can anyone tell me how I have to modify the code to make it bash compatible? eval `awk 'NR==3{print "set... (5 Replies)
Discussion started by: f_o_555
5 Replies
Login or Register to Ask a Question