Bash to select panel then specific file in directory

I am using bash to prompt a user for a choice using: where a "y" response opens a menu with available panels that can be used.

while true; do
    read -p "Do you want to get coverage of a specific panel?" yn
    case $yn in
        [Yy]* ) menu; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done 

My question is normally I would use a loop to apply the user selected panel to. However, in this case I can not do that because, there might be 5 files in the directory, but only 1 of those files gets the user selected panel applied to it, the other 4 do not.

For example, say the user selects panel1 and in the directory located at (/user/file/data) that panel1 can be applied to is file1 and file2 . How do I allow the user to select which file in the directory to apply panel1 to? Thank you :).

You would handle that within your menu function.
Try:

select file1 in $(cd /user/file/data;ls);do break;done
echo $file1

hth

1 Like

Is this what you mean? Thank you :).

while true; do
    read -p "Do you want to get coverage of a specific panel?" yn
    case $yn in
        [Yy]* ) menu; select file1 in $(cd /user/file/data;ls);do break;done
                  echo $file1
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done 

EDIT:
Obviously, instead of echo , do whatever you want/need to do with that selected file1 .

1 Like

The user prompt is executed and if "y" is entered then the menu function is called.

while true; do
    read -p "Do you want to get coverage of a specific panel?" yn
    case $yn in
        [Yy]* ) menu; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done 
menu() {
    while true
    do
        printf "\n please make a selection from the MENU \n
        ==================================
        \t 1  Incidental Findings     
  ==================================\n\n"
        printf "\t Your choice: "; read menu_choice

        case "$menu_choice" in
        1) incidental ;; select file1 in $(cd /user/file/data;ls);do break;done
            ......... (whatever command to execute)
        *) printf "\n Invalid choice."; sleep 2 ;;
        esac
    done
}

So if the "incidental" is selected after the ;; is where to add select file1 in $(cd /user/file/data;ls);do break;done ? Thank you :slight_smile:

Your above example will fail (just like the previous one).
Why do you put code in between cases?

After the ;; , the specific case is closed.
Before the *), no new case is opened.

Keep in mind, ; differs single/multiple commands from each other.
;; defines the end of a specific case of a case-block (from case to esac).

1) incidental ;; select file1 in $(cd /user/file/data;ls);do break;done
            ......... (whatever command to execute)
1) incidental ; select file1 in $(cd /user/file/data;ls);do break;done
            ......... (whatever command to execute) ;; 

hth, have a good day

1 Like

Thank you very much for your help and have a good day as well :).