bash 用户输入 if

发布于 2024-10-30 14:02:48 字数 270 浏览 5 评论 0原文

我正在尝试在 bash 中做一个简单的问题:

Do you want to do that? [Y,n] _

尝试过

echo "Do that? [Y,n]"
read DO_THAT
if ["DO_THAT"="y"]; then
  do_that
fi

,但失败了: bash: [y=y]: command not found

我做错了什么??!

I am trying to do a simple question in bash:

Do you want to do that? [Y,n] _

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

what am I doing wrong??!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

匿名。 2024-11-06 14:02:48

您可能会考虑显式提示:-p 并指定 1-character-input -n1,这允许在不输入 ENTER 的情况下插入 y。

read -n1 -p "Do that? [y,n]" doit 
case $doit in  
  y|Y) echo yes ;; 
  n|N) echo no ;; 
  *) echo dont know ;; 
esac

You might consider explicit prompting: -p and specifying 1-character-input -n1 which allows to insert y without ENTER.

read -n1 -p "Do that? [y,n]" doit 
case $doit in  
  y|Y) echo yes ;; 
  n|N) echo no ;; 
  *) echo dont know ;; 
esac
梦罢 2024-11-06 14:02:48
echo "Do that? [Y,n]"
read input
if [[ $input == "Y" || $input == "y" ]]; then
        echo "do that"
else
        echo "don't do that"
fi

密切注意 if 条件的语法和间距,它让我在 bash 中始终如此:)

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 :)

只是在用心讲痛 2024-11-06 14:02:48

在 bash 中查找要读取的选项 - 您可以进行提示等。

read -p "Do that? [Y,n]" -i Y input

其余的,在命令名称周围留出空格(“[”是一个命令 - 您甚至可以在 / 中找到它) bin/[(尽管它也是内置的 shell)和参数。


Bash 手册,第 4 章:Shell 内置命令

阅读

 读取 [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars]  
       [-p 提示符] [-t 超时] [-u fd] [名称...]

从标准输入或提供的文件描述符 fd 读取一行
作为“-u”选项的参数,第一个单词被分配给第一个
名字,第二个单词到第二个名字,依此类推,剩下的单词和
他们的中间分隔符分配给姓氏。如果字数少的话
从输入流中读取名称,其余名称被分配
空值。 IFS变量值中的字符用于分割
将行转化为文字。反斜杠字符“\”可用于删除任何
读取的下一个字符和行继续的特殊含义。如果没有
提供名称后,读取的行将分配给变量 REPLY。回报
代码为零,除非遇到文件结尾,否则读取超时(在这种情况下
返回代码大于 128),或者提供了无效的文件描述符作为
'-u' 的参数。

选项(如果提供)具有以下含义:

-a aname 将单词分配给数组变量的顺序索引
aname,从0开始。之前从aname中删除所有元素
作业。其他名称参数将被忽略。

-d delim delim的第一个字符用于终止输入行,
而不是换行符。
使用 -e Readline(参见第 8 章 [命令行编辑],第 93 页)
获得该线。 Readline 使用当前(或默认,if 行)
编辑以前未激活)编辑设置。
-i text 如果使用 Readline 读取该行,则文本将放入
在编辑开始之前编辑缓冲区。

-n nchars read 在读取 nchars 字符后返回而不是等待
对于完整的输入行,但如果少于
nchars 字符在分隔符之前读取。

-N nchars read 在准确读取 nchars 字符后返回,而不是
等待完整的一行输入,除非遇到 EOF 或
读取超时。输入中遇到的分隔符是
未进行特殊处理,并且不会导致 read 返回,直到 nchars
读取字符。

-p提示 在尝试之前显示提示,不带尾随换行符
读取任何输入。仅当有输入时才会显示提示
从终端。

-r 如果给出此选项,反斜杠不充当转义字符。
反斜杠被认为是该行的一部分。特别是,一个
反斜杠-换行符对不能用作续行符。

-s 静默模式。如果输入来自终端,则字符不会
回应。

-t 超时
如果输入完整行,则导致读取超时并返回失败
在超时秒内未读取。超时可能是小数
小数点后带有小数部分的数字。这
仅当 read 正在从终端读取输入时,选项才有效,
管道或其他特殊文件;从常规读取时没有效果
文件。如果超时为 0,如果输入可用,则读取返回成功
指定的文件描述符,否则失败。退出状态是
如果超过超时,则大于 128。

-u fd 从文件描述符 fd 读取输入。

Look up the options to read in bash - you can do the prompting etc.

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.


Bash Manual, Chapter 4: Shell Builtin Commands

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.

鲸落 2024-11-06 14:02:48

您需要在“if”和变量之间添加一个空格:

if [ $var == "y" ];然后
回声“eseera el Problema”

You need to put a blank Space between "if" and the variable:

if [ $var == "y" ]; then
echo "ese era el problema"
fi

静水深流 2024-11-06 14:02:48

您可以使用大小写修改运算符:

if [ "${DECISION^^}" = "Y" ]; then
...
fi

您可以在 bash 手册的“参数扩展”下找到更多信息。

You can use case modification operator:

if [ "${DECISION^^}" = "Y" ]; then
...
fi

You can find more in bash manual under "Parameter Expansion".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文