[英]Shell user prompt (Y/n)
I just wanted to write a small sript for copying some files for my NAS, so I'm not very experienced in Shell-Scripting.我只是想写一个小 sript 来为我的 NAS 复制一些文件,所以我在 Shell-Scripting 方面不是很有经验。 I know that many command line tools on Linux use the following sheme for Yes/No inputs
我知道 Linux 上的许多命令行工具对 Yes/No 输入使用以下方案
Are you yure [Y/n]
where the capitalized letter indicates the standard action which would also be started by hitting Enter .其中大写字母表示也可以通过按Enter开始的标准操作。 Which is nice for a quick usage.
这对于快速使用来说很好。
I also want to implement something like this, but I have some trouble with caching the Enter key.我也想实现这样的东西,但是我在缓存Enter键时遇到了一些麻烦。 Here is what I got so far:
这是我到目前为止所得到的:
read -p "Are you sure? [Y/n] " response
case $response in [yY][eE][sS]|[yY]|[jJ]|[#insert ENTER codition here#])
echo
echo files will be moved
echo
;;
*)
echo
echo canceld
echo
;;
esac
I can add what ever I want but it just won't work with Enter .我可以添加任何我想要的东西,但它不能与Enter 一起使用。
Here's a quick solution:这是一个快速的解决方案:
read -p "Are you sure? [Y/n] " response
case $response in [yY][eE][sS]|[yY]|[jJ]|'')
echo
echo files will be moved
echo
;;
*)
echo
echo canceled
echo
;;
esac
If you are using bash
4, you can "pre-seed" the response with the default answer, so that you don't have to treat ENTER
explicitly.如果您使用的是
bash
4,您可以使用默认答案“预先设定”响应,这样您就不必明确对待ENTER
。 (You can also standardize the case of response
to simplify the case
statement. (您还可以标准化
response
的case
以简化case
语句。
read -p "Are you sure? [Y/n] " -ei "y" response
response=${response,,} # convert to lowercase
case $response in
y|ye|yes)
echo
echo files will be moved
echo
;;
*)
echo
echo cancelled
echo
;;
You should use read -n1
你应该使用
read -n1
read -n1 -p "Are you sure? [Y/n] " response
case "$response" in
[yY]) echo "files will be moved";;
?) echo "canceled";;
esac
As per help read
:根据
help read
:
-n nchars return after reading NCHARS characters rather than waiting
for a newline, but honor a delimiter if fewer than NCHARS
characters are read before the delimiter
This has input validation that accepts "Y", "y", "an empty string" or "n" and "N" as valid input for the question [Y/n].这具有输入验证,接受“Y”、“y”、“空字符串”或“n”和“N”作为问题 [Y/n] 的有效输入。
#!/bin/bash
while : ; do # colon is built into bash; and is always true.
read -n1 -p "Are you sure? [Y/n] " response
echo
case "$response" in
y|Y|"") echo "files will be moved"; break ;; # Break out of while loop
n|N) echo -e "canceled"; break ;; # Break out of while loop
*) echo "Invalid option given." ;;
esac
done
Since you gave option [y/n], the code can be changed something like (edited):由于您提供了选项 [y/n],因此可以更改代码(已编辑):
#!/bin/bash
while true
do
echo "Are you sure? [Y/n]?"
read response
case $(echo ${response:-Y}|tr '[:lower:]' '[:upper:]') in
Y|YES)
echo "files will be moved!"
break
;;
N|NO)
echo "aborted!"
exit
;;
*)
echo "incorrect selection!"
;;
esac
done
A few years passed since the question was asked, but since it's still relevant in searches I thought I'd provide the different method I ended up using.自从提出这个问题以来已经过去了几年,但由于它仍然与搜索相关,我想我会提供我最终使用的不同方法。
I wanted to lock the question in place until ay/n/enter is pressed and without spamming the prompt request, so I (ab)used the -s
echo suppression and came up with the following:我想将问题锁定到位,直到按下 ay/n/enter 并且不发送提示请求的垃圾邮件,所以我(ab)使用了
-s
回声抑制并想出了以下内容:
#!/bin/bash
echo -n "Confirm action [y/N]? "
while true; do
read -sn1 response
case "$response" in
[yY]) echo 'y'; break;;
[nN]|"") echo 'n'; exit 0;;
esac
done
Upon N or Enter the script is ended, otherwise it just goes on as intended.在 N 或 Enter 时脚本结束,否则它会按预期继续。 The additional echos to output user choice is not necessary, but I left it in as a visual feedback.
输出用户选择的附加回声不是必需的,但我将其作为视觉反馈保留了下来。
Also, by moving the |""
around, you can default the enter key to either yes or no responses.此外,通过移动
|""
,您可以将回车键默认为是或否响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.