错误:=:找不到命令(Bash 脚本)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23065722/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:12:26  来源:igfitidea点击:

Error: =: command not found (Bash Script)

bash

提问by Brian Schroeter

I found a nifty little shell script that I wanted to use from this website here.I have followed everything step-by-step, but receive the following error when running this on my CentOS box.

我在这里找到了一个漂亮的小 shell 脚本,我想从这个网站上使用它。我已经一步一步地遵循了一切,但是在我的 CentOS 机器上运行它时收到以下错误。

./deploy: line 3: =: command not found

Line 3 only contains...

第 3 行仅包含...

$ERRORSTRING = "Error. Please make sure you've indicated correct parameters"

I've tried toying around with a bit, but don't understand why it won't accept the "=" character. Is there something wrong with the script, or is it merely something different in the way that my server processes the script?

我试过玩弄一下,但不明白为什么它不接受“=”字符。脚本是否有问题,或者只是我的服务器处理脚本的方式不同?

Thanks!

谢谢!

回答by Gordon Davisson

Gah, that script is full of bad scripting practices (in addition to the outright error you're running into). Here's the outright error:

哎呀,该脚本充满了糟糕的脚本编写实践(除了您遇到的彻底错误之外)。这是彻底的错误:

$ERRORSTRING = "Error. Please make sure you've indicated correct parameters"

As devnull pointed out, this should be:

正如 devnull 所指出的,这应该是:

ERRORSTRING="Error. Please make sure you've indicated correct parameters"

A couple of lines down (and again near the end), we have:

几行下来(再次接近尾声),我们有:

echo $ERRORSTRING;

...which works, but contains two bad ideas: a variable reference without double-quotes around it (which will sometimes be parsed in unexpected ways), and a semicolon at the end of the line (which is a sign that someone is trying to write C or Java or something in a shell script). Use this instead:

...它有效,但包含两个坏主意:一个没有双引号的变量引用(有时会以意想不到的方式解析),以及行尾的分号(这是某人正在尝试的标志)在shell脚本中编写C或Java或其他东西)。改用这个:

echo "$ERRORSTRING"

The next line is:

下一行是:

elif [  == "live" ]

...which mightwork, depending on whether the value of $1has spaces, or is defined-but-blank, or anything like that (again, use double-quotes to prevent misparsing!). Also, the == comparison operator is nonstandard -- it'll work, because bash supports it in its [ ... ]builtin syntax, but if you're counting on having bash extensions available, why not use the much cleaner [[ ... ]]replacement? Any of these would be a better replacement for that line:

...这可能会起作用,具体取决于 的值是否$1有空格,或已定义但为空,或类似的东西(再次,使用双引号来防止错误解析!)。此外,== 比较运算符是非标准的——它可以工作,因为 bash 的[ ... ]内置语法支持它,但是如果您指望有可用的 bash 扩展,为什么不使用更干净的[[ ... ]]替代品呢?这些中的任何一个都可以更好地替代该行:

elif [ "" = "live" ]
elif [[  == "live" ]]
elif [[ "" == "live" ]]

Personally, I prefer the last. The double-quotes aren't needed in this particular case, but IMO it's safest to just double-quote all variable references unless there's a specific reason not to. A bit further down, there's a elif [ $2 == "go" ]that the same comments apply to.

就我个人而言,我更喜欢最后一种。在这种特殊情况下不需要双引号,但 IMO 最安全的是双引号所有变量引用,除非有特定原因不这样做。再往下一点,有一个elif [ $2 == "go" ]同样的评论适用。

BTW, there's a good sanity-checking tool for shell scripts at www.shellcheck.net. It's not quite as picky as I am (e.g. it doesn't flag semicolons at the ends of lines), but it pointed out all of the actual errors in this script...

顺便说一句,www.shellcheck.net 上有一个很好的 shell 脚本健全性检查工具。它不像我那么挑剔(例如它没有在行尾标记分号),但它指出了这个脚本中的所有实际错误......

回答by Brian Schroeter

"Devnulls" answer was correct -- I had to remove the spaces around the "=" and remove the "$" from that line as well. The end result was...

“Devnulls”答案是正确的——我必须删除“=”周围的空格并从该行中删除“$”。最终的结果是……

ERRORSTRING="Error. Please make sure you've indicated correct parameters"

I've upvoted Devnull and gniourf_gniourf's comments.

我赞成 Devnull 和 gniourf_gniourf 的评论。

Thank you to all whom have assisted!

感谢所有帮助过的人!