【BASH】如何得到用户输入的YES或NO
来源:互联网 发布:unity3d培训机构 编辑:程序博客网 时间:2024/06/02 21:25
<!--@page { margin: 0.79in }P { margin-bottom: 0.08in }-->
问题:
你需要从用户那里得到简单的yes或no的输入,并且你期望做到用户界面的友好,做到能够区分大小写,并且提供用户点击Enter的默认选项。
解决方案
如果是简单的行为,可以使用下面这个函数。
#cookbook filename: func_choose
#Let the user make a choice about something and execute code based on
#the answer
#Called like: choose <default (y or n)> <prompt> <yesaction> <no action>
#e.g. choose "y" /
# "Do you want to play a game?" /
# /usr/games/GlobalThermonucularWar /
# 'printf "%b" "See you later Professor Falkin."'>&2
#Returns: nothing
functionchoose {
localdefault="$1"
localprompt="$2"
localchoice_yes="$3"
localchoice_no="$4"
localanswer
read-p "$prompt" answer
[-z "$answer" ] && answer="$default"
case"$answer" in
[yY1]) exec "$choice_yes"
#error check
;;
[nN0]) exec "$choice_no"
#error check
;;
* ) printf "%b" "Unexpected answer '$answer'!" >&2 ;;
esac
}# end of function choose
如果是复杂的操作,可以使用下面的函数
#cookbook filename: func_choice.1
#Let the user make a choice about something and return a standardized
#answer. How the default is handled and what happens next is up to
#the if/then after the choice in main
#Called like: choice <promtp>
#e.g. choice "Do you want to play a game?"
$Returns: global variable CHOICE
functionchoice {
CHOICE=''
localprompt="$*"
localanswer
read-p "$prompt" answer
case"$answer" in
[yY1]) CHOICE='y';;
[nN0]) CHOICE='n';;
* ) CHOICE="$answer";;
esac
}# end of function choice
下面这段代码调用choice函数,提示用户确认时间。假设$THISPACKAGE已经设定,将显示时间并要求用户确认,如果用户选择y,Y,或者Enter,那么这个时间就被接受,如果用户输入一个新的时间,函数将循环并且要求用户确认。
#cookbook filename: func_choice.2
until[ "$CHOICE" = "y" ]; do
printf"%b" "This package's date is $THISPACKAGE/n" >&2
choice"Is that correct? [Y/,<New date>]: "
if[ -z "$CHOICE" ]; then
CHOICE='y'
elif[ "$CHOICE" != "y" ]; then
printf"%b" "Overriding $THISPACKAGEwith ${CHOICE}/n"
THISPACKAGE=$CHOICE
fi
done
#Build the package here
接下来,使用其他方法来解决这个问题。用户通过认真阅读提示和查看默认行为,可以简单的点击Enter,脚本将执行默认操作。
#If the user types anything except a case insensitive 'n', they will
#see the error log
choice"Do you want to look at the error log file? [Y/n]: "
if[ "$choice" != "n" ]; then
lesserror.log
fi
#If the user types anything except a case insensitive 'y', they will
#not see the message log
choice"Do you want to look at the message log file? [y/N]: "
if[ "$choice" = "y" ]; then
lessmessage.log
fi
最后,如果用户没有输入,可以使用下面的方法解决
#cookbook filename: func_choice.3
choice"Enter your favorite color, if you have one: "
if[ -n "$CHOICE" ]; then
printf"%b" "You chose: $CHOICE"
else
printf"%b" "You do not have a favorite color."
fi
- 【BASH】如何得到用户输入的YES或NO
- linux中使用ssh或scp时如何跳过RSA key fingerprint输入yes/no
- linux中使用ssh或scp时如何跳过RSA key fingerprint输入yes/no
- linux中使用ssh或scp时如何跳过RSA key fingerprint输入yes/no
- 【BASH】如何读取用户输入
- 如何让脚本自己输入密码或者yes no
- 将输入的string中"yes" 改为 "no"
- C#控制台基础 用do-while循环限制用户只能输入yes或者no
- 得到用户或语句权限的信息
- 如何得到 TextBox 或 richTextBox 的当前输入光标的屏幕坐标
- 执行shell脚本时,遇到需要输入yes/no的时候,怎么自动化完成输入呢?
- 自己的YES OR NO
- 如何得到或设置操作系统的语言
- 将输入的string中的"yes" 改为"no"
- 如何从用户得到短的文本?
- 如何得到Oracle的所有用户?
- 如何控制用户的输入
- 得到用户的输入并显示到控制台
- 使用FEST-Swing测试GUI
- Test on quota
- 第一篇
- 小知识
- THU APR 16,2009
- 【BASH】如何得到用户输入的YES或NO
- 怎样使可编辑的DIV固定大小,当内容多时,就自动出现滚动条
- 解决Cygwin与eclipse没有include的问题
- 关于EditPlus正则表达式替换字符串详解
- zz: PS已经不是唯一的照片处理方式了~(轉)很有用的說咯 看完記得頂下./
- 如何为Delphi程序添加事件和事件处理器
- Ehcache入门指南
- 鼠标屏幕取词 原理
- vb.net中向SQLSERVE数据库存取图片(流的形式)