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

时间:2013-03-02 11:51:42

标签: bash scripting zsh prompt

使用zsh,我试图在我的~/.zprofile中加入一个步骤,在那里我交互式地询问是/否样式问题。起初我尝试this bash-style approach,但我看到了这种形式的错误:

read: -p: no coprocess

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

此页面暗示语法可能不同,因此在this page和zsh手册页的引导下,我尝试了这样做:

read -q 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?"

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

4 个答案:

答案 0 :(得分:18)

来自zsh - read

  

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

你必须引用整个论点

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

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

如果您不引用问号,zsh会尝试将参数与文件名匹配。如果它找不到任何匹配的文件名,则会向no matches found抱怨。

答案 1 :(得分:5)

我要添加此答案,因为每次您要询问用户确认时,您都还想对其采取行动。这是一个函数,它会用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
}

答案 2 :(得分:4)

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

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

答案 3 :(得分:0)

我为此创建了两个实用程序脚本:

  • read-string.sh 从输入中读取字符串(需要用户按 Enter)
  • read-char.sh 从输入中读取单个字符(不需要按 Enter)

无论用户使用的是 bash 还是 zsh,这两个脚本都可以正常工作。

#!/bin/bash
# read-string.sh
# eg: my_string=$(./read-string.sh); echo "my_string: $my_string"

# bash `read` manual - https://ss64.com/bash/read.html
# 
# read [-ers] [-a aname]  [-d delim] [-i text] [-n nchars]
#    [-N nchars] [-p prompt] [-r] [-s] [-t timeout] [-u fd]
#       [name...]
# 
#  -r        Do not treat a Backslash as an escape character.  The backslash is considered to be part
#            of the line. In particular, a backslash-newline pair can not be used as a line continuation.
#            Without this option, any backslashes in the input will be discarded.
#            You should almost always use the -r option with read.

# zsh `read` manual - http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html#index-read
# 
# read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
#     [ -u n ] [ name[?prompt] ] [ name ... ]
# 
# -r         Raw mode: a ‘\’ at the end of a line does not signify line continuation and backslashes in the line
#            don’t quote the following character and are not removed.

if [ -n "$ZSH_VERSION" ]; then
  read -r "answer?"
else
  read -r answer
fi
echo "$answer"
#!/bin/bash
# eg: my_char=$(read-char.sh); echo "my_char: $my_char"

# bash `read` manual - https://ss64.com/bash/read.html
# 
# read [-ers] [-a aname]  [-d delim] [-i text] [-n nchars]
#    [-N nchars] [-p prompt] [-r] [-s] [-t timeout] [-u fd]
#       [name...]
# 
#  -r        Do not treat a Backslash as an escape character.  The backslash is considered to be part
#            of the line. In particular, a backslash-newline pair can not be used as a line continuation.
#            Without this option, any backslashes in the input will be discarded.
#            You should almost always use the -r option with read.
#  -n nchars read returns after reading nchars characters rather than waiting for a complete line of input.

# zsh `read` manual - http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html#index-read
# 
# read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
#     [ -u n ] [ name[?prompt] ] [ name ... ]
# 
# -q         Read only one character from the terminal and set name to ‘y’ if this character was ‘y’ or ‘Y’
#            and to ‘n’ otherwise. With this flag set the return status is zero only if the character was ‘y’ or ‘Y’.
#            This option may be used with a timeout (see -t); if the read times out, or encounters end of file,
#            status 2 is returned. Input is read from the terminal unless one of -u or -p is present.
#            This option may also be used within zle widgets.
# -r         Raw mode: a ‘\’ at the end of a line does not signify line continuation and backslashes in the line
#            don’t quote the following character and are not removed.

if [ -n "$ZSH_VERSION" ]; then
  read -r -q "answer?"
else
  read -r -n 1 answer
fi
echo "$answer"

感谢Olaforiginal answer