bash 如何在 zsh 脚本中提示是/否样式确认?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15174121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:43:32  来源:igfitidea点击:

How can I prompt for yes/no style confirmation in a zsh script?

bashscriptingzshprompt

提问by Andrew Ferrier

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

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

read: -p: no coprocess

read: -p: no coprocess

(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 语法与 bash 不同 - 我尝试在它前面加上 sh 仿真命令emulate -LR sh- 但没有区别)。

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

这个页面暗示语法可能不同,所以在这个页面和 zsh 手册页的指导下,我尝试了这个:

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

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

This instead fails with an error of the form:

这反而失败并出现以下形式的错误:

/home/user/.zprofile:5: no matches found: REPLY?"This is the question I want to ask?"

/home/user/.zprofile:5: no matches found: REPLY?"This is the question I want to ask?"

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 问一个简单的是/否问题?理想情况下,该命令只会吞下一个字符,无需按 Enter/Return,并且是“安全的”——即,除非输入“Y”或“y”,否则后续测试默认为 no/false。

回答by Olaf Dietsche

From zsh - read

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.

如果第一个参数包含一个“?”,当 shell 是交互式的时,这个词的其余部分用作标准错误的提示。

You must quote the entire argument

你必须引用整个论点

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

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

这将提示您This is the question I want to ask?并返回按下的字符REPLY

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

如果您不引用问号,则zsh尝试将参数作为文件名进行匹配。如果它没有找到任何匹配的文件名,它会用no matches found.

回答by GoZoner

See ZSH Manualfor documentation of ZSH's read. Try:

有关ZSH阅读的文档,请参阅ZSH 手册。尝试:

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

回答by Ben

I'm adding this answer because every time you want to ask the user for confirmation, you also want to act on it. here's a function that prompts with read -q(thanks, other answers!) and branches on the result to do what you want (in this case, git stuff):

我添加这个答案是因为每次你想要求用户确认时,你也想对它采取行动。这是一个提示read -q(谢谢,其他答案!)并在结果上进行分支以执行您想要的操作的函数(在本例中为 git 内容):

git_commit_and_pull() {
    # http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html#index-read
    if read -q "choice?Press Y/y to continue with commit and pull: "; then
        set -x
        git add . && git commit -m 'haha git goes brrr' && git pull
        { set +x; } 2>/dev/null
    else
        echo
        echo "'$choice' not 'Y' or 'y'. Exiting..."
    fi
}