bash - Comment puis-je activer Yes/Non/Annuler l'entrée dans un script shell Linux

Mots clés : linuxbashshellscriptingposixncurseskshdashbusyboxlinux

meilleur 5 Réponses bash - Comment puis-je activer Yes/Non/Annuler l'entrée dans un script shell Linux

vote vote

95

while true; do     read -p "Do you wish to install this program?" yn     case $yn in         [Yy]* ) make install; break;;         [Nn]* ) exit;;         * ) echo "Please answer yes or no.";;     esac done 
echo "Do you wish to install this program?" select yn in "Yes" "No"; do     case $yn in         Yes ) make install; break;;         No ) exit;;     esac done 
set -- $(locale LC_MESSAGES) yesptrn="$1"; noptrn="$2"; yesword="$3"; noword="$4"  while true; do     read -p "Install (${yesword} / ${noword})? " yn     if [[ "$yn" =~ $yesexpr ]]; then make install; exit; fi     if [[ "$yn" =~ $noexpr ]]; then exit; fi     echo "Answer ${yesword} / ${noword}." done 
vote vote

81

echo -n "Is this a good question (y/n)? " read answer 
# if echo "$answer" | grep -iq "^y" ;then 
if [ "$answer" != "${answer#[Yy]}" ] ;then # this grammar (the #[] operator) means that the variable $answer where any Y or y in 1st position will be dropped if they exist.     echo Yes else     echo No fi 
echo -n "Is this a good question (y/n)? " old_stty_cfg=$(stty -g) stty raw -echo ; answer=$(head -c 1) ; stty $old_stty_cfg # Careful playing with stty if echo "$answer" | grep -iq "^y" ;then     echo Yes else     echo No fi 
#/bin/sh echo -n "Is this a good question (y/n)? " old_stty_cfg=$(stty -g) stty raw -echo answer=$( while ! head -c 1 | grep -i '[ny]' ;do true ;done ) stty $old_stty_cfg if echo "$answer" | grep -iq "^y" ;then     echo Yes else     echo No fi 
if whiptail --yesno "Is this a good question" 20 60 ;then     echo Yes else     echo No fi 
dialog --yesno "Is this a good question" 20 60 && echo Yes  gdialog --yesno "Is this a good question" 20 60 && echo Yes  kdialog --yesno "Is this a good question" 20 60 && echo Yes 
DIALOG=whiptail if [ -x /usr/bin/gdialog ] ;then DIALOG=gdialog ; fi if [ -x /usr/bin/xdialog ] ;then DIALOG=xdialog ; fi ... $DIALOG --yesno ... 
read -p "Is this a good question (y/n)? " answer case ${answer:0:1} in     y|Y )         echo Yes     ;;     * )         echo No     ;; esac 
read -n 1 -p "Is this a good question (y/n)? " answer 
read -t 3 -n 1 -p "Is this a good question (y/n)? " answer [ -z "$answer" ] && answer="Yes"  # if 'yes' have to be default choice 
dialog --menu "Is this a good question" 20 60 12 y Yes n No m Maybe 
dialog --gauge "Filling the tank" 20 60 0 < <(     for i in {1..100};do         printf "XXX\n%d\n%(%a %b %T)T progress: %d\nXXX\n" $i -1 $i         sleep .033     done )  
#!/bin/sh while true ;do     [ -x "$(which ${DIALOG%% *})" ] || DIALOG=dialog     DIALOG=$($DIALOG --menu "Which tool for next run?" 20 60 12 2>&1 \             whiptail       "dialog boxes from shell scripts" >/dev/tty \             dialog         "dialog boxes from shell with ncurses" \             gdialog        "dialog boxes from shell with Gtk" \             kdialog        "dialog boxes from shell with Kde" ) || exit     clear;echo "Choosed: $DIALOG."     for i in `seq 1 100`;do         date +"`printf "XXX\n%d\n%%a %%b %%T progress: %d\nXXX\n" $i $i`"         sleep .0125       done | $DIALOG --gauge "Filling the tank" 20 60 0     $DIALOG --infobox "This is a simple info box\n\nNo action required" 20 60     sleep 3     if $DIALOG --yesno  "Do you like this demo?" 20 60 ;then         AnsYesNo=Yes; else AnsYesNo=No; fi     AnsInput=$($DIALOG --inputbox "A text:" 20 60 "Text here..." 2>&1 >/dev/tty)     AnsPass=$($DIALOG --passwordbox "A secret:" 20 60 "First..." 2>&1 >/dev/tty)     $DIALOG --textbox /etc/motd 20 60     AnsCkLst=$($DIALOG --checklist "Check some..." 20 60 12 \         Correct "This demo is useful"        off \         Fun        "This demo is nice"        off \         Strong        "This demo is complex"        on 2>&1 >/dev/tty)     AnsRadio=$($DIALOG --radiolist "I will:" 20 60 12 \         " -1" "Downgrade this answer"        off \         "  0" "Not do anything"                on \         " +1" "Upgrade this anser"        off 2>&1 >/dev/tty)     out="Your answers:\nLike: $AnsYesNo\nInput: $AnsInput\nSecret: $AnsPass"     $DIALOG --msgbox "$out\nAttribs: $AnsCkLst\nNote: $AnsRadio" 20 60   done 
#!/bin/bash  set -i HISTFILE=~/.myscript.history history -c history -r  myread() {     read -e -p '> ' $1     history -s ${!1} } trap 'history -a;exit' 0 1 2 3 6  while myread line;do     case ${line%% *} in         exit )  break ;;         *    )  echo "Doing something with '$line'" ;;       esac   done 
vote vote

74

echo "Please enter some input: " read input_variable echo "You entered: $input_variable" 
vote vote

60

read -e -p "Enter the path to the file: " -i "/usr/local/etc/" FILEPATH echo $FILEPATH 
read -e -p " List the content of your home dir ? [Y/n] " YN  [[ $YN == "y" || $YN == "Y" || $YN == "" ]] && ls -la ~/ 
vote vote

59

select result in Yes No Cancel do     echo $result done 

Questions similaires