23

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

read: -p: no coprocess

(我知道 zsh 语法通常与 bash 不同——我尝试在它前面加上一个 sh 仿真命令emulate -LR sh——但它没有区别)。

这个页面暗示语法可能不同,所以在这个页面和 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

4 回答 4

33

zsh - 读取

如果第一个参数包含“?”,则该单词的其余部分将在 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.

于 2013-03-02T12:46:36.320 回答
12

我添加这个答案是因为每次你想要求用户确认时,你也想对它采取行动。这是一个提示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
        git add . && git commit -m 'haha git goes brrr' && git pull
    else
        echo
        echo "'$choice' not 'Y' or 'y'. Exiting..."
    fi
}
于 2020-04-21T21:26:30.443 回答
9

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

read REPLY\?"This is the question I want to ask?"
于 2013-03-02T13:48:34.550 回答
1

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

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

无论用户使用的是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"

谢奥拉夫原始回答

于 2021-04-22T23:21:13.650 回答