如何在zsh脚本中提示是/否样式确认? [英] How can I prompt for yes/no style confirmation in a zsh script?

查看:51
本文介绍了如何在zsh脚本中提示是/否样式确认?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用zsh,我试图在我的〜/.zprofile 中迈出一步,以交互方式询问是/否样式问题.最初,我尝试了这种bash风格的方法,但是我看到了这种形式的错误:

Using zsh, I'm trying to put a step in my ~/.zprofile where I interactively ask a yes/no style question. At first I tried this bash-style approach, but I saw errors of this form:

读取:-p:无协同处理

(我知道zsh语法通常与bash的语法不同-我尝试在其前面使用sh仿真命令- emulate -LR sh -但没有区别).

(I'm aware that typically the zsh syntax is different from bash's - I tried preceding it with a sh emulation command - emulate -LR sh - but it made no difference).

此页面暗示语法可能有所不同,因此请按照此页面和在zsh手册页中,我尝试了以下方法:

This page implied the syntax might be different, so guided by this page and the zsh man page, I tried this instead:

已读-q答复?"这是我想问的问题吗?"

这会失败,并出现以下形式的错误:

This instead fails with an error of the form:

/home/user/.zprofile:5:找不到匹配项:REPLY?这是我想问的问题吗?"

如何用zsh问一个简单的是/否问题?理想情况下,该命令只需吞下一个字符,无需按Enter/Return键即可,并且是安全的"-也就是说,除非输入"Y"或"y",否则后续测试默认为no/false.

How can I ask a simple yes/no question with zsh? Ideally the command would just swallow one character, with no need to press Enter/Return, and be 'safe' - i.e. the subsequent test defaults to no/false unless 'Y' or 'y' are entered.

推荐答案

来自zsh-阅读

如果第一个参数包含?",则该词的其余部分将在外壳是交互式的情况下用作标准错误的提示.

If the first argument contains a ‘?’, the remainder of this word is used as a prompt on standard error when the shell is interactive.

您必须引用整个参数

read -q "REPLY?This is the question I want to ask?"

这将提示您这是我要问的问题吗?,并返回在 REPLY 中按下的字符.

this will prompt you with This is the question I want to ask? and return the character pressed in REPLY.

如果不引号,则 zsh 尝试将参数作为文件名进行匹配.而且,如果找不到任何匹配的文件名,则会抱怨未找到匹配项.

If you don't quote the question mark, zsh tries to match the argument as a filename. And if it doesn't find any matching filename, it complains with no matches found.

这篇关于如何在zsh脚本中提示是/否样式确认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆