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 overwritten each other. That is if there are 4 files to be processed, only the first gets processed then it gets caught in a loop and overwrites and 2,3,4 never even start. It seems like panel not being called is what the issue is, but I am not sure why its not being called. The .... are additional lines in the script. Thank you :).

Bash

execute() {
    while true
    do
# oldest folder stored as variable for analysis, version log created, and quality indicators matched to run
dir=/home/cmccabe/Desktop/NGS/API

find "$dir" -maxdepth 1 -mindepth 1 -type d -printf '%T+\t%P\0' | sort -rz |
while read -r -d $'\t' time && read -r -d '' filename
do
    printf "The oldest folder is $filename, created on $time and analysis done using v1.7 by $USER at $(date "+%D %r")\n" >> /home/cmccabe/medex.logs/folder.log
    awk -v FL="$filename" '
         FNR == 1 {filenum++}
         filenum==1 && index($0, FL) { 
              match($0, "_0*([0-9]+)/")
              FNUM=substr($0,RSTART+1,RLENGTH-2)
              gsub(/^0+/,"", FNUM)
          }
          #filenum==2 && $0 ~ FNUM".pdf$"' /home/cmccabe/s5_files/downloads/list /home/cmccabe/s5_files/pdf/pdf > /home/cmccabe/s5_files/pdf/output
    break
done
...
...
...
...
panel
done
}

menu() {
    while true
    do
        printf "\n please make a selection from the MENU \n
        ==================================
        \t 1  gene
        \t 2  test
        ==================================\n\n"
        printf "\t Your choice: "; read menu_choice
        echo "$menu_coice panel selected"

        case "$menu_choice" in
        1) gene ;;
        2) test ;;
        *) printf "\n Invalid choice."; sleep 2 ;;
        esac
    done
}


while true; do
    read -p "Do you want to start the me?" yn
    case $yn in
        [Yy]* ) execute; break ;;
        [Nn]* ) exit ;;
        * ) echo "Please answer yes or no.";;
    esac
done

panel() {
    printf "Do you want to get coverage of a specific gene, genes, or panel?"; read yn
    case $yn in
        [Yy]* ) menu ;;
        [Nn]* ) move ;;
        * ) echo "Please answer yes or no.";;
    esac
}

I'm not sure if this is a rule. But I generally write the functions before i call them. Can you try putting panel() before execute() and run your code.

2 Likes

Hi.

I didn't look at the entire code, but it seems to be a rule in bash , ksh , zsh , and dash :

#!/usr/bin/env bash

# @(#) s1       Demonstrate placement of function definitions.

LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C

a() {
  printf " Hello, inside function a\n"
}

pl " Executing function a:"
a

pl " Executing function b before definition:"
b

b() {
  printf " Hello, inside function b\n"
}


pl " Executing function b after definition:"
b

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30

-----
 Executing function a:
 Hello, inside function a

-----
 Executing function b before definition:
./s1: line 21: b: command not found

-----
 Executing function b after definition:
 Hello, inside function b

Good catch balajesuri !

Best wishes ... cheers, drl

2 Likes

Thank you both for your help :).