while true; doread -p "Do you wish to install this program? " yncase $yn in[Yy]* ) make install; break;;[Nn]* ) exit;;* ) echo "Please answer yes or no.";;esacdone
#!/bin/bashif (dialog --title "Message" --yesno "Want to do something risky?" 6 25)# message box will have the size 25x6 charactersthenecho "Let's do something risky"# do something riskyelseecho "Let's stay boring"fi
uniprompt(){while true; doecho -e "$1\c"read optarray=($2)case "${array[@]}" in *"$opt"*) eval "$3=$opt";return 0;; esacecho -e "$opt is not a correct value\n"done}
像这样使用它:
unipromtp "Select an option: (a)-Do one (x)->Do two (f)->Do three : " "a x f" selectionecho "$selection"
printf '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 Yeselseecho Nofi
if whiptail --yesno "Is this a good question" 20 60 ;thenecho Yeselseecho Nofi
根据您的系统,您可能需要将whiptail替换为另一个类似的工具:
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
do_xxxx=y # In batch mode => Default is Yes[[ -t 0 ]] && # If TTY => Prompt the questionread -n 1 -p $'\e[1;32mDo xxxx? (Y/n)\e[0m ' do_xxxx # Store the answer in $do_xxxxif [[ $do_xxxx =~ ^(y|Y|)$ ]] # Do if 'y' or 'Y' or emptythenxxxxfi
#! /bin/sh
# For potential users of BSD, or other systems who do not# have a bash binary located in /bin the script will be directed to# a bourne-shell, e.g. /bin/sh
# NOTE: It would seem best for handling user entry errors or# exceptions, to put the decision required by the input# of the prompt in a case statement (case control structure),
echo Would you like us to perform the option: "(Y|N)"
read inPut
case $inPut in# echoing a command encapsulated by# backticks (``) executes the command"Y") echo `Do something crazy`;;# depending on the scenario, execute the other option# or leave as default"N") echo `execute another option`;;esac
exit
ans=''while true; doread -p "So is MikeQ the greatest or what (y/n/q) ?" -n1 anscase ${ans,,} iny|n|q) break;;*) echo "Answer y for yes / n for no or q for quit.";;esacdone
echo -e "\nAnswer = $ans"
if [[ "${ans,,}" == "q" ]] ; thenecho "OK Quitting, we will assume that he is"exit 0fi
if [[ "${ans,,}" == "y" ]] ; thenecho "MikeQ is the greatest!!"elseecho "No? MikeQ is not the greatest?"fi
$ get_yes_keypress "Here you cannot press enter. Do you like this [y/n]? "Here you cannot press enter. Do you like this [y/n]? kHere you cannot press enter. Do you like this [y/n]?Here you cannot press enter. Do you like this [y/n]? n$ echo $?1
这里返回了1或false。请注意,使用此低级函数,您需要提供自己的[y/n]?提示符。
代码
# Read a single char from /dev/tty, prompting with "$*"# Note: pressing enter will return a null string. Perhaps a version terminated with X and then remove it in caller?# See https://unix.stackexchange.com/a/367880/143394 for dealing with multi-byte, etc.function get_keypress {local REPLY IFS=>/dev/tty printf '%s' "$*"[[ $ZSH_VERSION ]] && read -rk1 # Use -u0 to read from STDIN# See https://unix.stackexchange.com/q/383197/143394 regarding '\n' -> ''[[ $BASH_VERSION ]] && </dev/tty read -rn1printf '%s' "$REPLY"}
# Get a y/n from the user, return yes=0, no=1 enter=$2# Prompt using $1.# If set, return $2 on pressing enter, useful for cancel or defualtingfunction get_yes_keypress {local prompt="${1:-Are you sure [y/n]? }"local enter_return=$2local REPLY# [[ ! $prompt ]] && prompt="[y/n]? "while REPLY=$(get_keypress "$prompt"); do[[ $REPLY ]] && printf '\n' # $REPLY blank if user presses entercase "$REPLY" inY|y) return 0;;N|n) return 1;;'') [[ $enter_return ]] && return "$enter_return"esacdone}
# Credit: http://unix.stackexchange.com/a/14444/143394# Prompt to confirm, defaulting to NO on <enter># Usage: confirm "Dangerous. Are you sure?" && rm *function confirm {local prompt="${*:-Are you sure} [y/N]? "get_yes_keypress "$prompt" 1}
# Prompt to confirm, defaulting to YES on <enter>function confirm_yes {local prompt="${*:-Are you sure} [Y/n]? "get_yes_keypress "$prompt" 0}
#!/usr/bin/env sh
# Getting LC_MESSAGES values into variables# shellcheck disable=SC2046 # Intended IFS splittingIFS='' set -- $(locale LC_MESSAGES)
yesexpr="$1"noexpr="$2"yesstr="$3"nostr="$4"messages_codeset="$5" # unused here, but kept as documentation
# Display Yes / No ? prompt into localeecho "$yesstr / $nostr ?"
# Read answerread -r yn
# Test answercase "$yn" in# match only work with the character class from the expression${yesexpr##^}) echo "answer $yesstr" ;;${noexpr##^}) echo "answer $nostr" ;;esac