Answers:
该命令是在shell提示符下获取用户输入的最简单,使用最广泛的方法read
。演示其用法的最佳方法是一个简单的演示:
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
史蒂芬·休伊格(Steven Huwig)指出的另一种方法是Bash的命令。这是使用的相同示例:select
select
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
随着select
你并不需要净化输入-它显示可用的选项,你键入相应的你的选择一个号码。它还会自动循环,因此,while true
如果输入无效,则无需重试循环。
此外,LEA格里斯表现出一种方法,使在请求语言无关她的回答。修改我的第一个示例以更好地服务于多种语言可能看起来像这样:
set -- $(locale LC_MESSAGES)
yesptrn="$1"; noptrn="$2"; yesword="$3"; noword="$4"
while true; do
read -p "Install (${yesword} / ${noword})? " yn
case $yn in
${yesptrn##^} ) make install; break;;
${noptrn##^} ) exit;;
* ) echo "Answer ${yesword} / ${noword}.";;
esac
done
显然,这里没有翻译其他通信字符串(安装,回答),这需要通过更完整的翻译来解决,但是在许多情况下,即使是部分翻译也将有所帮助。
exit
为break
在选择“否”时不关闭选项卡。
break
在select
如果没有循环?
取决于
如果你想
您可以使用以下read
命令if ... then ... else
:
echo -n "Is this a good question (y/n)? "
read answer
# if echo "$answer" | grep -iq "^y" ;then
if [ "$answer" != "${answer#[Yy]}" ] ;then
echo Yes
else
echo No
fi
(感谢Adam Katz的评论:用更便携并且避免了一个分叉的测试代替了上面的测试:)
但是,如果您不希望用户点击Return,则可以这样写:
(编辑:正如@JonathanLeffler正确建议的那样,保存 stty的配置可能比简单地强制他们保持理智更好。)
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
相同,但显式等待y或n:
#/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
有很多其使用内置的工具libncurses
,libgtk
,libqt
或其他图形库。例如,使用whiptail
:
if whiptail --yesno "Is this a good question" 20 60 ;then
echo Yes
else
echo No
fi
根据您的系统,您可能需要替换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
其中,20
是以行数为单位的对话框高度,是对话框的60
宽度。这些工具都具有几乎相同的语法。
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
我更喜欢使用,case
所以我什至可以测试yes | ja | si | oui
是否需要...
在bash下,我们可以为read
命令指定预期输入的长度:
read -n 1 -p "Is this a good question (y/n)? " answer
在bash下,read
命令接受一个超时参数,这可能很有用。
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
除简单yes - no
目的外,更复杂的对话框:
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
更多样品?看看使用鞭子选择USB设备和USB可移动存储选择器:USBKeyChooser
例:
#!/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
这将创建一个文件.myscript.history
在$HOME
目录中,比你能使用输入行的历史命令,比如Up, Down,Ctrl+ r等。
stty
提供了-g
使用选项:old_stty=$(stty -g); stty raw -echo; …; stty "$old_stty"
。这将完全恢复找到的设置,该设置可能与可能相同stty -sane
。
case
POSIX和bash一起使用(使用通配符条件而不是bash子字符串:)case $answer in; [Yy]* ) echo Yes ;;
,但是我更喜欢使用条件语句,而不是[ "$answer" != "${answer#[Yy]}" ]
您的echo "$answer" | grep -iq ^y
。它更可移植(某些非GNU greps无法-q
正确实现),并且没有系统调用。 ${answer#[Yy]}
使用参数扩展来删除Y
或y
从的开头开始($answer
如果存在),则导致不平等。这适用于任何POSIX Shell(破折号,ksh,bash,zsh,busybox等)。
echo "Please enter some input: "
read input_variable
echo "You entered: $input_variable"
您可以使用内置的read命令;使用该-p
选项提示用户一个问题。
从BASH4开始,您现在可以使用-i
以下建议:
read -e -p "Enter the path to the file: " -i "/usr/local/etc/" FILEPATH
echo $FILEPATH
(但是请记住使用“ readline”选项-e
来允许使用箭头键编辑行)
如果您想使用“是/否”逻辑,则可以执行以下操作:
read -e -p "
List the content of your home dir ? [Y/n] " YN
[[ $YN == "y" || $YN == "Y" || $YN == "" ]] && ls -la ~/
FILEPATH
是您选择的变量名,并通过命令提示符的答案进行设置。因此vlc "$FILEPATH"
,例如如果要运行,vlc
将打开该文件。
-e
在第二个示例中(简单的是/否)有什么好处?
-e -p
有-ep
什么理由要使用?
-e
标志/选项,您可能(取决于实现)可能无法键入“ y”,然后改变主意并用“ n”(或与此相关的其他任何内容)替换;在记录命令时,除其他原因外,出于可读性/清晰度的考虑,单独列出选项更好。
Bash 为此选择了。
select result in Yes No Cancel
do
echo $result
done
exit
内部:)
Ctrl-D
。但是,当然,使用它的实际代码将需要在体内断裂或退出。)
exit
将一起退出脚本,break
仅退出您所在的循环(如果您位于“ while
或” case
循环上)
这是我整理的:
#!/bin/sh
promptyn () {
while true; do
read -p "$1 " yn
case $yn in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
* ) echo "Please answer yes or no.";;
esac
done
}
if promptyn "is the sky blue?"; then
echo "yes"
else
echo "no"
fi
我是一个初学者,所以可以加一点盐,但是似乎有效。
case $yn in
为,case ${yn:-$2} in
则可以将第二个参数用作默认值Y或
case $yn
到case "${yn:-Y}"
有是默认
inquire () {
echo -n "$1 [y/n]? "
read answer
finish="-1"
while [ "$finish" = '-1' ]
do
finish="1"
if [ "$answer" = '' ];
then
answer=""
else
case $answer in
y | Y | yes | YES ) answer="y";;
n | N | no | NO ) answer="n";;
*) finish="-1";
echo -n 'Invalid response -- please reenter:';
read answer;;
esac
fi
done
}
... other stuff
inquire "Install now?"
...
do_xxxx=y # In batch mode => Default is Yes
[[ -t 0 ]] && # If TTY => Prompt the question
read -n 1 -p $'\e[1;32m
Do xxxx? (Y/n)\e[0m ' do_xxxx # Store the answer in $do_xxxx
if [[ $do_xxxx =~ ^(y|Y|)$ ]] # Do if 'y' or 'Y' or empty
then
xxxx
fi
[[ -t 0 ]] && read ...
=> read
如果是TTY,则调用命令read -n 1
=>等待一个字符$'\e[1;32m ... \e[0m '
=>以绿色打印[[ $do_xxxx =~ ^(y|Y|)$ ]]
=> bash正则表达式do_xxxx=y
[[ -t 0 ]] && { # Timeout 5 seconds (read -t 5)
read -t 5 -n 1 -p $'\e[1;32m
Do xxxx? (Y/n)\e[0m ' do_xxxx || # read 'fails' on timeout
do_xxxx=n ; } # Timeout => answer No
if [[ $do_xxxx =~ ^(y|Y|)$ ]]
then
xxxx
fi
用最少的行数来实现此目的的最简单方法如下:
read -p "<Your Friendly Message here> : y/n/cancel" CONDITION;
if [ "$CONDITION" == "y" ]; then
# do something here!
fi
该if
仅仅是一个例子:它是由你如何处理这个变量。
使用read
命令:
echo Would you like to install? "(Y or N)"
read x
# now check if $x is "y"
if [ "$x" = "y" ]; then
# do something here!
fi
然后您需要的所有其他东西
要获得类似ncurses的漂亮输入框,请使用如下命令对话框:
#!/bin/bash
if (dialog --title "Message" --yesno "Want to do something risky?" 6 25)
# message box will have the size 25x6 characters
then
echo "Let's do something risky"
# do something risky
else
echo "Let's stay boring"
fi
可以在POSIX Shell中处理区域设置感知的“是/否选择”。通过使用LC_MESSAGES
语言环境类别的条目,witch提供了现成的RegEx模式以匹配输入,并提供了用于本地化的字符串。是否。
#!/usr/bin/env sh
# Getting LC_MESSAGES values into variables
# shellcheck disable=SC2046 # Intended IFS splitting
IFS='
' 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 locale
echo "$yesstr / $nostr ?"
# Read answer
read -r yn
# Test answer
case "$yn" in
# match only work with the character class from the expression
${yesexpr##^}) echo "answer $yesstr" ;;
${noexpr##^}) echo "answer $nostr" ;;
esac
编辑:正如@Urhixidur在他的评论中提到的:
不幸的是,POSIX只指定了前两个(yesexpr和noexpr)。在Ubuntu 16上,yesstr和nostr为空。
参见:https : //www.ee.ryerson.ca/~courses/ele709/susv4/xrat/V4_xbd_chap07.html#tag_21_07_03_06
LC_MESSAGES
在
yesstr
与nostr
现场的关键字和YESSTR
和NOSTR
langinfo项目以前用于匹配用户的肯定和否定响应。在POSIX.1-2008的yesexpr
,noexpr
,YESEXPR
,和NOEXPR
扩展正则表达式的替代者。应用程序应使用基于常规语言环境的常规消息传递功能来发出提示消息,其中包括所需的示例响应。
或者使用Bash方式使用语言环境:
#!/usr/bin/env bash
IFS=$'\n' read -r -d '' yesexpr noexpr _ < <(locale LC_MESSAGES)
printf -v yes_or_no_regex "(%s)|(%s)" "$yesexpr" "$noexpr"
printf -v prompt $"Please answer Yes (%s) or No (%s): " "$yesexpr" "$noexpr"
declare -- answer=;
until [[ "$answer" =~ $yes_or_no_regex ]]; do
read -rp "$prompt" answer
done
if [[ -n "${BASH_REMATCH[1]}" ]]; then
echo $"You answered: Yes"
else
echo $"No, was your answer."
fi
使用语言环境环境提供的正则表达式来匹配答案。
要翻译其余消息,请使用bash --dump-po-strings scriptname
输出po字符串进行本地化:
#: scriptname:8
msgid "Please answer Yes (%s) or No (%s): "
msgstr ""
#: scriptname:17
msgid "You answered: Yes"
msgstr ""
#: scriptname:19
msgid "No, was your answer."
msgstr ""
yesexpr
,并noexpr
在shell环境,是用它击的特定正则表达式匹配if [[ "$yn" =~ $yesexpr ]]; then echo $"Answered yes"; else echo $"Answered no"; fi
这是一个更长的但可重用的模块化方法:
0
= y和1
= nozsh
和bash
。请注意,N
大写。在这里按下回车键,接受默认值:
$ confirm "Show dangerous command" && echo "rm *"
Show dangerous command [y/N]?
另请注意,这[y/N]?
是自动附加的。默认的“否”被接受,因此没有回声。
重新提示,直到给出有效的响应:
$ confirm "Show dangerous command" && echo "rm *"
Show dangerous command [y/N]? X
Show dangerous command [y/N]? y
rm *
请注意,将Y
大写:
$ confirm_yes "Show dangerous command" && echo "rm *"
Show dangerous command [Y/n]?
rm *
在上面,我只是按下Enter键,所以命令运行了。
y
或n
$ 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]? k
Here 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 -rn1
printf '%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 defualting
function get_yes_keypress {
local prompt="${1:-Are you sure [y/n]? }"
local enter_return=$2
local REPLY
# [[ ! $prompt ]] && prompt="[y/n]? "
while REPLY=$(get_keypress "$prompt"); do
[[ $REPLY ]] && printf '\n' # $REPLY blank if user presses enter
case "$REPLY" in
Y|y) return 0;;
N|n) return 1;;
'') [[ $enter_return ]] && return "$enter_return"
esac
done
}
# 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
}
Show dangerous command [y/N]? [y/n]?
和Show dangerous command [Y/n]? [y/n]?
很抱歉在这样的旧帖子上发帖。几周前,我遇到了类似的问题,就我而言,我需要一个可以在在线安装脚本中使用的解决方案,例如:curl -Ss https://raw.github.com/_____/installer.sh | bash
使用read yesno < /dev/tty
对我来说很好:
echo -n "These files will be uploaded. Is this ok? (y/n) "
read yesno < /dev/tty
if [ "x$yesno" = "xy" ];then
# Yes
else
# No
fi
希望这对某人有帮助。
tty
那样适应我的第一个示例来接受输入,对您来说也会做得很好,并且还会循环输入错误的输入(想象一下缓冲区中的几个字符;您的方法将迫使用户始终选择否)。
我注意到没有人发布如此简单的用户输入显示多行回显菜单的答案,因此,我可以这样做:
#!/bin/bash
function ask_user() {
echo -e "
#~~~~~~~~~~~~#
| 1.) Yes |
| 2.) No |
| 3.) Quit |
#~~~~~~~~~~~~#\n"
read -e -p "Select 1: " choice
if [ "$choice" == "1" ]; then
do_something
elif [ "$choice" == "2" ]; then
do_something_else
elif [ "$choice" == "3" ]; then
clear && exit 0
else
echo "Please select 1, 2, or 3." && sleep 3
clear && ask_user
fi
}
ask_user
发布此方法是为了希望有人发现它有用且节省时间。
多选版本:
ask () { # $1=question $2=options
# set REPLY
# options: x=..|y=..
while $(true); do
printf '%s [%s] ' "$1" "$2"
stty cbreak
REPLY=$(dd if=/dev/tty bs=1 count=1 2> /dev/null)
stty -cbreak
test "$REPLY" != "$(printf '\n')" && printf '\n'
(
IFS='|'
for o in $2; do
if [ "$REPLY" = "${o%%=*}" ]; then
printf '\n'
break
fi
done
) | grep ^ > /dev/null && return
done
}
例:
$ ask 'continue?' 'y=yes|n=no|m=maybe'
continue? [y=yes|n=no|m=maybe] g
continue? [y=yes|n=no|m=maybe] k
continue? [y=yes|n=no|m=maybe] y
$
它将设置REPLY
为y
(在脚本内部)。
我建议你使用对话框 ...
Linux学徒:使用对话框改进Bash Shell脚本
对话框命令使您可以在外壳程序脚本中使用窗口框,以使它们的使用更具交互性。
它简单易用,还有一个名为gdialog的gnome版本,它具有完全相同的参数,但在X上显示了GUI样式。
受到@Mark和@Myrddin答案的启发,我为通用提示创建了此功能
uniprompt(){
while true; do
echo -e "$1\c"
read opt
array=($2)
case "${array[@]}" in *"$opt"*) eval "$3=$opt";return 0;; esac
echo -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" selection
echo "$selection"
更通用的是:
function menu(){
title="Question time"
prompt="Select:"
options=("Yes" "No" "Maybe")
echo "$title"
PS3="$prompt"
select opt in "${options[@]}" "Quit/Cancel"; do
case "$REPLY" in
1 ) echo "You picked $opt which is option $REPLY";;
2 ) echo "You picked $opt which is option $REPLY";;
3 ) echo "You picked $opt which is option $REPLY";;
$(( ${#options[@]}+1 )) ) clear; echo "Goodbye!"; exit;;
*) echo "Invalid option. Try another one.";continue;;
esac
done
return
}
一种简单的方法是with xargs -p
或gnu parallel --interactive
。
为此,我更喜欢xargs的行为,因为它像其他交互式unix命令一样,在提示符后立即执行每个命令,而不是收集yeses来最后运行。(完成所需的操作后,可以按Ctrl-C。)
例如,
echo *.xml | xargs -p -n 1 -J {} mv {} backup/
xargs --interactive
仅限于是或否。只要满足您的需求就足够了,但是我最初的问题举了一个例子,给出了三种可能的结果。我真的很喜欢它,尽管它很流。许多常见的情况都将受益于它的管道传输能力。
作为单行命令的朋友,我使用了以下命令:
while [ -z $prompt ]; do read -p "Continue (y/n)?" choice;case "$choice" in y|Y ) prompt=true; break;; n|N ) exit 0;; esac; done; prompt=;
书面的长格式,它是这样的:
while [ -z $prompt ];
do read -p "Continue (y/n)?" choice;
case "$choice" in
y|Y ) prompt=true; break;;
n|N ) exit 0;;
esac;
done;
prompt=;
case
在这种情况下,我已经使用过几次该语句,使用case语句是解决该问题的好方法。甲while
环,即ecapsulates的case
块,利用一个布尔条件可以为了保持程序的甚至更多的控制,并且满足许多其它要求来实现。满足所有条件后,break
可以使用a 将控制权交还给程序的主要部分。同样,为了满足其他条件,当然可以添加条件语句以伴随控制结构:case
语句和可能的while
循环。
使用case
语句满足您的请求的示例
#! /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
#!/usr/bin/env bash
@confirm() {
local message="$*"
local result=''
echo -n "> $message (Yes/No/Cancel) " >&2
while [ -z "$result" ] ; do
read -s -n 1 choice
case "$choice" in
y|Y ) result='Y' ;;
n|N ) result='N' ;;
c|C ) result='C' ;;
esac
done
echo $result
}
case $(@confirm 'Confirm?') in
Y ) echo "Yes" ;;
N ) echo "No" ;;
C ) echo "Cancel" ;;
esac
#!/usr/bin/env bash
@confirm() {
local message="$*"
local result=3
echo -n "> $message (y/n) " >&2
while [[ $result -gt 1 ]] ; do
read -s -n 1 choice
case "$choice" in
y|Y ) result=0 ;;
n|N ) result=1 ;;
esac
done
return $result
}
if @confirm 'Confirm?' ; then
echo "Yes"
else
echo "No"
fi
回应他人:
您无需在BASH4中指定大小写,只需使用','即可使var小写。我也非常不喜欢将代码放入读取块中,获取结果并在读取块IMO之外进行处理。还包括退出海事组织的“ q”。最后,为什么键入'yes'只是使用-n1并按y键。
示例:用户可以按y / n和q退出。
ans=''
while true; do
read -p "So is MikeQ the greatest or what (y/n/q) ?" -n1 ans
case ${ans,,} in
y|n|q) break;;
*) echo "Answer y for yes / n for no or q for quit.";;
esac
done
echo -e "\nAnswer = $ans"
if [[ "${ans,,}" == "q" ]] ; then
echo "OK Quitting, we will assume that he is"
exit 0
fi
if [[ "${ans,,}" == "y" ]] ; then
echo "MikeQ is the greatest!!"
else
echo "No? MikeQ is not the greatest?"
fi
在大多数情况下,您需要继续执行脚本,直到用户继续输入“是”为止,并且仅在用户输入“否”时才停止。以下代码段将帮助您实现这一目标!
#!/bin/bash
input="yes"
while [ "$input" == "yes" ]
do
echo "execute script functionality here..!!"
echo "Do you want to continue (yes/no)?"
read input
done
[yn]
选项,则大写的选项是默认的,即[Yn]
默认为“是”,[yN]
默认为“否”。参见ux.stackexchange.com/a/40445/43532