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 :).

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.

...
...
...
...
 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
}

You cannot nest function declarations, the contents of run() belong outside the contents of execute().

1 Like

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 :).

 ...
...
...
...
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
}
 

'break' is meaningless outside a while loop, remove it instead of the ;;

There should also be a space before ;;

1 Like

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).

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 :).

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