Adding to a menu

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

    printf "\t Your choice: "; read menu_choice

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


    ==================================



         1  Match patient

                 2 Sanger analysis


         3  Exit

Trying to add menu items but it appears to be misaligned and II'm not sure why. Thank you :).

It's clearly misaligned in your source -- not too mysterious. Maybe your text editor and the terminal disagree on how many spaces a tab is.

The shell has a built in menu tool which may make your life simpler:

clear ; printf "%s\n" "MENU" "========================"

# This is not just a menu, it's a loop.  It keeps asking until you
# break out with 'break' or 'exit'.
select X in "Match patient" "Sanger analysis" "Exit"
do
        case "$REPLY" in
        1)      match  ;;
        2)      sanger ;;
        3)      exit   ;;
        esac

        clear ; printf "%s\n" "MENU" "========================"
done
1 Like

But - you have an extra <TAB> in your printf statement. What now is unclear?

How about creating the menu in a text file and cat ting that out before reading the choice?

1 Like

I guess RudiC is right.

    \t 2  Sanger analysis\n\n

I think you have two tab stops here infront of "\t". Try it with a single tab.

Alternatively delete the whole line containing menu option 2, copy the previous line and paste it in the (new) next (empty) line and replace the text.

Hope I made myself clear.

1 Like

What about

 dialog

?

Regards,
xabbu

1 Like
$ dialog
-bash: dialog: command not found
$

select, on the other hand, is a built-in in most modern shells.

1 Like

Yes, you are right.

Regards,
xabbu

1 Like

Thank you all :slight_smile:

My old favorite using select and an array:

#!/bin/bash
# select menu with array example
# 2014.11.13 by sea
################### 
#
#	Variables
#
	ar_menu=( "Menu entry 1" "Menu entry 2" "Menu entry 3" "Menu entry 4" )
#
#	Display & Action
#
	echo "Please select your action:"
	select menu in Back "${ar_menu[@]}";do
		echo "Selected: $menu"
		case "$menu" in
		Back)			break			;;
		"${ar_menu[0]}")	echo "menu action 1"	;;
		"${ar_menu[1]}")	echo "menu action 2"	;;
		"${ar_menu[2]}")	echo "menu action 3"	;;
		"${ar_menu[3]}")	echo "menu action 4"	;;
		esac
		echo "Press [ENTER] to see the menu:"
	done

Hope this helps to get started

EDIT:
Which modified could look like:

#!/bin/bash 
#
#	Variables
#
	ar_menu=( "Match patient" "Sanger analysis" ) 
#
#	Display & Action
#
	echo "Please select your action:"
	select menu in Back "${ar_menu[@]}";do
		echo "Selected: $menu"
		case "$menu" in
		Back)			break					;;
		"${ar_menu[0]}")	echo "Function to look up patient"	;;
		"${ar_menu[1]}")	echo "Do the sanger analyisis"		;;
		*)			echo "Invalid choice!!"			;;
		esac
		printf '%s\n' "-------------------" "Press [ENTER] to see the menu:"
	done

And would output like:

sh 1.sh 
Please select your action:
1) Back
2) Match patient
3) Sanger analysis
#? 2
Selected: Match patient
Function to look up patient
-------------------
Press [ENTER] to see the menu:
#? 3
Selected: Sanger analysis
Do the sanger analyisis
-------------------
Press [ENTER] to see the menu:
#? 4
Selected: 
Invalid choice!!
-------------------
Press [ENTER] to see the menu:
#? 1
Selected: Back
1 Like