Unix Shell Scripting Comparing Strings的这些格式有什么问题

时间:2015-06-18 12:55:14

标签: bash unix

我一直在阅读关于脚本编写的StackExchange帖子,而且我所尝试的一切都没有像我期望的那样工作。

这也是我的第一个shell脚本。

# Script File Begin
echo "Enter yes | no"
read uInput

if ["$uInput" != "n"]; then
  echo "Yes"
fi

问题在于,无论我如何尝试if条件,我都会遇到line 5: n: command not found

我尝试了不同的迭代,例如

[["$uInput" != "n*"]]
[["$uInput" != n*]]
[[$uInput != n*]]
["$uInput" != "n*"]
["$uInput" != n*]
[$uInput != "n*"]
[$uInput != n*]

但他们都告诉我第5行:n:命令未找到。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

Bash中的空格至关重要,不是可选的。将if语句更改为:

if [ "$uInput" != "n" ]; then

<小时/> 如果你想知道为什么?看到这个:

Why should be there a space after '[' and before ']' in the Bash Script