bash 用户输入如果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5542016/
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
bash user input if
提问by frnhr
I am tring to do simple
我想做简单的事
Do you want to do that? [Y,n] _
question in bash.
bash 中的问题。
i tried
我试过
echo "Do that? [Y,n]"
read DO_THAT
if ["DO_THAT"="y"]; then
do_that
fi
but it fails: bash: [y=y]: command not found
但它失败了: bash: [y=y]: command not found
what am I doing wrong??!
我究竟做错了什么??!
回答by user unknown
You might consider explicit prompting: -p and specifying 1-character-input -n1 which allows to insert y without ENTER.
您可能会考虑显式提示:-p 并指定 1-character-input -n1 允许插入 y 而无需 ENTER。
read -n1 -p "Do that? [y,n]" doit
case $doit in
y|Y) echo yes ;;
n|N) echo no ;;
*) echo dont know ;;
esac
回答by Kenny Cason
echo "Do that? [Y,n]"
read input
if [[ $input == "Y" || $input == "y" ]]; then
echo "do that"
else
echo "don't do that"
fi
Pay close attention to the syntax and spacing of the if conditional, it gets me all the time in bash :)
密切注意 if 条件的语法和间距,它让我一直在 bash 中:)
回答by Jonathan Leffler
Look up the options to read
in bash - you can do the prompting etc.
read
在 bash 中查找选项- 您可以进行提示等。
read -p "Do that? [Y,n]" -i Y input
For the rest, leave spaces around command names ('[' is a command - you might even find it in /bin/[
though it is also a shell built-in) and arguments.
其余的,在命令名称('[' 是一个命令 -/bin/[
尽管它也是一个内置的 shell)和参数之间留出空格。
Bash Manual, Chapter 4: Shell Builtin Commands
Bash 手册,第 4 章:Shell 内置命令
read
read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
One line is read from the standard input, or from the file descriptor fd supplied as an argument to the ‘-u' option, and the first word is assigned to the first name, the second word to the second name, and so on, with leftover words and their intervening separators assigned to the last name. If there are fewer words read from the input stream than names, the remaining names are assigned empty values. The characters in the value of the IFS variable are used to split the line into words. The backslash character ‘\' may be used to remove any special meaning for the next character read and for line continuation. If no names are supplied, the line read is assigned to the variable REPLY. The return code is zero, unless end-of-file is encountered, read times out (in which case the return code is greater than 128), or an invalid file descriptor is supplied as the argument to ‘-u'.
Options, if supplied, have the following meanings:
-a aname The words are assigned to sequential indices of the array variable aname, starting at 0. All elements are removed from aname before the assignment. Other name arguments are ignored.
-d delim The first character of delim is used to terminate the input line, rather than newline. -e Readline (see Chapter 8 [Command Line Editing], page 93) is used to obtain the line. Readline uses the current (or default, if line editing was not previously active) editing settings. -i text If Readline is being used to read the line, text is placed into the editing buffer before editing begins.
-n nchars read returns after reading nchars characters rather than waiting for a complete line of input, but honor a delimiter if fewer than nchars characters are read before the delimiter.
-N nchars read returns after reading exactly nchars characters rather than waiting for a complete line of input, unless EOF is encountered or read times out. Delimiter characters encountered in the input are not treated specially and do not cause read to return until nchars characters are read.
-p prompt Display prompt, without a trailing newline, before attempting to read any input. The prompt is displayed only if input is coming from a terminal.
-r If this option is given, backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslash-newline pair may not be used as a line continuation.
-s Silent mode. If input is coming from a terminal, characters are not echoed.
-t timeout Cause read to time out and return failure if a complete line of input is not read within timeout seconds. timeout may be a decimal number with a fractional portion following the decimal point. This option is only effective if read is reading input from a terminal, pipe, or other special file; it has no effect when reading from regular files. If timeout is 0, read returns success if input is available on the specified file descriptor, failure otherwise. The exit status is greater than 128 if the timeout is exceeded.
-u fd Read input from file descriptor fd.
读
read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
一行是从标准输入中读取的,或者从作为参数提供给 '-u' 选项的文件描述符 fd 中读取,第一个单词分配给第一个名称,第二个单词分配给第二个名称,依此类推,剩余的单词及其中间的分隔符分配给姓氏。如果从输入流中读取的单词少于名称,则剩余的名称将被分配空值。IFS 变量值中的字符用于将行拆分为单词。反斜杠字符 '\' 可用于删除下一个读取字符和行继续的任何特殊含义。如果未提供名称,则将读取的行分配给变量 REPLY。返回码为零,除非遇到文件尾,读取超时(在这种情况下返回码大于128),
选项(如果提供)具有以下含义:
-a aname 将单词分配给数组变量 aname 的顺序索引,从 0 开始。在分配之前从 aname 中删除所有元素。其他名称参数将被忽略。
-d delim delim 的第一个字符用于终止输入行,而不是换行符。-e Readline(参见第 8 章 [命令行编辑],第 93 页)用于获取行。Readline 使用当前(或默认,如果行编辑以前未处于活动状态)编辑设置。-i text 如果使用 Readline 读取该行,则在开始编辑之前将文本放入编辑缓冲区。
-n nchars read 在读取 nchars 个字符后返回,而不是等待完整的输入行,但如果在分隔符之前读取的字符少于 nchars 个字符,则使用分隔符。
-N nchars read 在完全读取 nchars 个字符后返回,而不是等待完整的输入行,除非遇到 EOF 或读取超时。输入中遇到的分隔符不会被特殊处理,并且在读取 nchars 个字符之前不会导致 read 返回。
-p prompt 在尝试读取任何输入之前显示提示,没有尾随换行符。仅当输入来自终端时才显示提示。
-r 如果给出这个选项,反斜杠不作为转义字符。反斜杠被认为是行的一部分。特别是,反斜杠-换行符对不能用作换行符。
-s 静默模式。如果输入来自终端,则不回显字符。
-t timeout 如果在 timeout 秒内未读取完整的输入行,则导致读取超时并返回失败。timeout 可以是一个十进制数,小数点后面有小数部分。此选项仅在 read 从终端、管道或其他特殊文件读取输入时有效;从常规文件读取时它不起作用。如果 timeout 为 0,如果输入在指定的文件描述符上可用,则 read 返回成功,否则返回失败。如果超时,则退出状态大于 128。
-u fd 从文件描述符 fd 读取输入。
回答by Louis Eduardo Arcila Bonfante
You need to put a blank Space between "if" and the variable:
您需要在“if”和变量之间放置一个空格:
if [ $var == "y" ]; then
echo "ese era el problema"
fi
if [ $var == "y" ]; then
echo "ese era el problema"
fi
回答by niziak
You can use case modification operator:
您可以使用案例修改运算符:
if [ "${DECISION^^}" = "Y" ]; then
...
fi
You can find more in bash manual under "Parameter Expansion".
您可以在“参数扩展”下的 bash 手册中找到更多信息。