找不到 bash 输出命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9831768/
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 output command not found
提问by john doe
Im facing following problem I have created mentioned condition, but when I choose y for yes everything is ok, but when I choose n for not I get annoying error output: output : Do you agree yes (y) or not (n) n ./myscript: [n: command not found
我面临以下问题,我已经创建了提到的条件,但是当我选择 y 表示是时一切正常,但是当我选择 n 表示不时,我会得到烦人的错误输出:输出:您同意是 (y) 还是不同意 (n) n 。 /myscript: [n: 命令未找到
myscript is the name of my script Code here:
myscript 是我的脚本代码的名称在这里:
echo "Do you agree yes (y) or not (n)"
read answer
if ( [ "$answer" = 'y' ] || ["$answer" = 'Y' ]);
then
echo -e “ output for y”
done
else
echo -e " output for n "
exit 1;
Any idea how can I get rid of the output and fix the problem ? thanks
知道如何摆脱输出并解决问题吗?谢谢
采纳答案by John Zwinck
That's not bash. "done" does not terminate an "if" condition in bash. You should remove "done" and add "fi" at the end of the else body.
那不是bash。“完成”不会终止 bash 中的“if”条件。您应该删除“完成”并在 else 正文的末尾添加“fi”。
Also, the semicolon after "exit 1" is not needed.
此外,不需要“exit 1”后面的分号。
回答by stanwise
You missed the space in:
您错过了以下空间:
["$answer" = 'Y' ]
Change to:
改成:
[ "$answer" = 'Y' ]
There are also other mistakes in the script. Here you have working code:
脚本中还有其他错误。在这里你有工作代码:
echo "Do you agree yes (y) or not (n)"
read answer
if ( [[ "$answer" = 'y' ]] || [[ "$answer" = 'Y' ]]);
then
echo -e " output for y"
else
echo -e " output for n"
exit 1
fi
回答by FatalError
You are missing a space after the [in your second condition. [is actually a command and since it's together it tries to literally run [n. You don't see the output with ybecause the evaluation is short circuited (i.e. the first condition is true so there's no need to evaluate the second).
您[在第二个条件中缺少一个空格。 [实际上是一个命令,因为它在一起,它试图从字面上运行[n. 您看不到输出,y因为评估是短路的(即第一个条件为真,因此无需评估第二个)。
回答by johnshen64
done should be after the else clause, i.e., before exit. and it is "fi" not "done".
done 应该在 else 子句之后,即在退出之前。它是“fi”而不是“done”。

