183 votes

Comment puis-je créer un menu de sélection dans un Shell Shell ?

Je suis en train de créer un simple bash script et je veux y créer un menu de sélection, comme ceci :

$./script

echo "Choose your option:"

1) Option 1  
2) Option 2  
3) Option 3  
4) Quit  

Et selon le choix de l'utilisateur, je veux que différentes actions soient exécutées. Je suis un noob du scripting bash Shell, j'ai cherché des réponses sur le web, mais je n'ai rien obtenu de vraiment concret.

10voto

Alex Lucard Points 109
#!/bin/sh
show\_menu(){
    normal=\`echo "\\033\[m"\`
    menu=\`echo "\\033\[36m"\` #Blue
    number=\`echo "\\033\[33m"\` #yellow
    bgred=\`echo "\\033\[41m"\`
    fgred=\`echo "\\033\[31m"\`
    printf "\\n${menu}\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*${normal}\\n"
    printf "${menu}\*\*${number} 1)${menu} Mount dropbox ${normal}\\n"
    printf "${menu}\*\*${number} 2)${menu} Mount USB 500 Gig Drive ${normal}\\n"
    printf "${menu}\*\*${number} 3)${menu} Restart Apache ${normal}\\n"
    printf "${menu}\*\*${number} 4)${menu} ssh Frost TomCat Server ${normal}\\n"
    printf "${menu}\*\*${number} 5)${menu} Some other commands${normal}\\n"
    printf "${menu}\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*${normal}\\n"
    printf "Please enter a menu option and enter or ${fgred}x to exit. ${normal}"
    read opt
}

option\_picked(){
    msgcolor=\`echo "\\033\[01;31m"\` # bold red
    normal=\`echo "\\033\[00;00m"\` # normal white
    message=${@:-"${normal}Error: No message passed"}
    printf "${msgcolor}${message}${normal}\\n"
}

clear
show\_menu
while \[ $opt != '' \]
    do
    if \[ $opt = '' \]; then
      exit;
    else
      case $opt in
        1) clear;
            option\_picked "Option 1 Picked";
            printf "sudo mount /dev/sdh1 /mnt/DropBox/; #The 3 terabyte";
            show\_menu;
        ;;
        2) clear;
            option\_picked "Option 2 Picked";
            printf "sudo mount /dev/sdi1 /mnt/usbDrive; #The 500 gig drive";
            show\_menu;
        ;;
        3) clear;
            option\_picked "Option 3 Picked";
            printf "sudo service apache2 restart";
            show\_menu;
        ;;
        4) clear;
            option\_picked "Option 4 Picked";
            printf "ssh lmesser@ -p 2010";
            show\_menu;
        ;;
        x)exit;
        ;;
        \\n)exit;
        ;;
        \*)clear;
            option\_picked "Pick an option from the menu";
            show\_menu;
        ;;
      esac
    fi
done

7voto

alastairs Points 3045

Étant donné que ceci est destiné à Ubuntu, vous devriez utiliser le backend que debconf est configuré pour utiliser. Vous pouvez trouver le backend de debconf avec :

sudo -s "echo get debconf/frontend | debconf-communicate"

S'il est écrit "dialogue", alors il est probable qu'il utilise whiptail o dialog . Sur Lucid, c'est whiptail .

Si cela échoue, utilisez "select" de bash comme expliqué par Dennis Williamson.

7voto

user360154 Points 79

Menu fantaisie Bash

Essayez-le d'abord, puis visitez ma page pour une description détaillée ... Pas besoin de bibliothèques externes ou de programmes comme dialog ou zenity ...

#/bin/bash
# by oToGamez
# www.pro-toolz.net

      E='echo -e';e='echo -en';trap "R;exit" 2
    ESC=$( $e "\e")
   TPUT(){ $e "\e[${1};${2}H";}
  CLEAR(){ $e "\ec";}
  CIVIS(){ $e "\e[?25l";}
   DRAW(){ $e "\e%@\e(0";}
  WRITE(){ $e "\e(B";}
   MARK(){ $e "\e[7m";}
 UNMARK(){ $e "\e[27m";}
      R(){ CLEAR ;stty sane;$e "\ec\e[37;44m\e[J";};
   HEAD(){ DRAW
           for each in $(seq 1 13);do
           $E "   x                                          x"
           done
           WRITE;MARK;TPUT 1 5
           $E "BASH SELECTION MENU                       ";UNMARK;}
           i=0; CLEAR; CIVIS;NULL=/dev/null
   FOOT(){ MARK;TPUT 13 5
           printf "ENTER - SELECT,NEXT                       ";UNMARK;}
  ARROW(){ read -s -n3 key 2>/dev/null >&2
           if [[ $key = $ESC[A ]];then echo up;fi
           if [[ $key = $ESC[B ]];then echo dn;fi;}
     M0(){ TPUT  4 20; $e "Login info";}
     M1(){ TPUT  5 20; $e "Network";}
     M2(){ TPUT  6 20; $e "Disk";}
     M3(){ TPUT  7 20; $e "Routing";}
     M4(){ TPUT  8 20; $e "Time";}
     M5(){ TPUT  9 20; $e "ABOUT  ";}
     M6(){ TPUT 10 20; $e "EXIT   ";}
      LM=6
   MENU(){ for each in $(seq 0 $LM);do M${each};done;}
    POS(){ if [[ $cur == up ]];then ((i--));fi
           if [[ $cur == dn ]];then ((i++));fi
           if [[ $i -lt 0   ]];then i=$LM;fi
           if [[ $i -gt $LM ]];then i=0;fi;}
REFRESH(){ after=$((i+1)); before=$((i-1))
           if [[ $before -lt 0  ]];then before=$LM;fi
           if [[ $after -gt $LM ]];then after=0;fi
           if [[ $j -lt $i      ]];then UNMARK;M$before;else UNMARK;M$after;fi
           if [[ $after -eq 0 ]] || [ $before -eq $LM ];then
           UNMARK; M$before; M$after;fi;j=$i;UNMARK;M$before;M$after;}
   INIT(){ R;HEAD;FOOT;MENU;}
     SC(){ REFRESH;MARK;$S;$b;cur=`ARROW`;}
     ES(){ MARK;$e "ENTER = main menu ";$b;read;INIT;};INIT
  while [[ "$O" != " " ]]; do case $i in
        0) S=M0;SC;if [[ $cur == "" ]];then R;$e "\n$(w        )\n";ES;fi;;
        1) S=M1;SC;if [[ $cur == "" ]];then R;$e "\n$(ifconfig )\n";ES;fi;;
        2) S=M2;SC;if [[ $cur == "" ]];then R;$e "\n$(df -h    )\n";ES;fi;;
        3) S=M3;SC;if [[ $cur == "" ]];then R;$e "\n$(route -n )\n";ES;fi;;
        4) S=M4;SC;if [[ $cur == "" ]];then R;$e "\n$(date     )\n";ES;fi;;
        5) S=M5;SC;if [[ $cur == "" ]];then R;$e "\n$($e by oTo)\n";ES;fi;;
        6) S=M6;SC;if [[ $cur == "" ]];then R;exit 0;fi;;
 esac;POS;done

6voto

Nasir Points 2363

J'ai utilisé Zenity, qui semble toujours présent dans Ubuntu, fonctionne très bien et a de nombreuses capacités. Voici une esquisse d'un menu possible :

#! /bin/bash

selection=$(zenity --list "Option 1" "Option 2" "Option 3" --column="" --text="Text above column(s)" --title="My menu")

case "$selection" in
"Option 1")zenity --info --text="Do something here for No1";;
"Option 2")zenity --info --text="Do something here for No2";;
"Option 3")zenity --info --text="Do something here for No3";;
esac

3voto

Joel Spolsky Points 22686

La même question se pose déjà dans défaut du serveur répondu. La solution utilisée ici est la suivante queue de fouet .

SistemesEz.com

SystemesEZ est une communauté de sysadmins où vous pouvez résoudre vos problèmes et vos doutes. Vous pouvez consulter les questions des autres sysadmins, poser vos propres questions ou résoudre celles des autres.

Powered by:

X