bash 用户输入 if
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能会考虑显式提示:-p 并指定 1-character-input -n1,这允许在不输入 ENTER 的情况下插入 y。
You might consider explicit prompting: -p and specifying 1-character-input -n1 which allows to insert y without ENTER.
密切注意 if 条件的语法和间距,它让我在 bash 中始终如此:)
Pay close attention to the syntax and spacing of the if conditional, it gets me all the time in bash :)
在 bash 中查找要读取的选项 - 您可以进行提示等。
其余的,在命令名称周围留出空格(“[”是一个命令 - 您甚至可以在
/ 中找到它) bin/[
(尽管它也是内置的 shell)和参数。Bash 手册,第 4 章:Shell 内置命令
Look up the options to
read
in bash - you can do the prompting etc.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
您需要在“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
您可以使用大小写修改运算符:
您可以在 bash 手册的“参数扩展”下找到更多信息。
You can use case modification operator:
You can find more in bash manual under "Parameter Expansion".