Bash if 或语句,找不到命令

标签 bash shell ubuntu

我对这行 shell 脚本有疑问:

if [ "$(lsb_release -r -s)" == "16.04" ] || [ "$(lsb_release -r -s)" == "17.04" ]; then

运行脚本时出现“[: command not found”。我不懂为什么。
+ '[' -f /etc/debian_version ']'
++ lsb_release -r -s
+ '[' 17.04 == 16.04 ']'
++ lsb_release -r -s
+ ' [' 17.04 == 17.04 ']'
./mhn/scripts/install_mongo.sh: line 8:  [: command not found

如果在 17.04 上运行,我只会收到错误,而不是 16.04。如果我切换语句并首先检查 17.04,它将在 16.04 中断,而不是 17.04。声明后半部分的某些内容正在打破。

https://github.com/ngatilio/mhn/blob/2e992934a350e0367a214dc86cc63e7ecd5d59ef/scripts/install_mongo.sh

最佳答案

你的问题是:

+ ' [' 17.04 == 17.04 ']'

在最后一个 [ 之前,您似乎有某种时髦的空格字符。 .删除并重新键入它以将其变成常规的 ascii 空间。

当您在此处复制粘贴该行时,或者通过非原始 github View 查看它时,它会被转换为常规空间,因此它不会出现在您的帖子中。这也是为什么它在复制粘贴到另一个文件时可以正常工作的原因。

这是从 github 下载(而不是复制粘贴)的文件的 ShellCheck:
$ shellcheck install_mongo.sh

In install_mongo.sh line 8:
    if [ "$(lsb_release -r -s)" == "16.04" ] || [ "$(lsb_release -r -s)" == "17.04" ]; then
                                               ^-- SC1018: This is a unicode non-breaking space. Delete and retype it.

关于Bash if 或语句,找不到命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46354006/

相关文章:

node.js - fs.existsSync 不是 Node 错误中的函数

python - 用 python 自动化 firefox?

c++ - 在 Ubuntu 上的 QT5 中将透明 QWidget 放在 QMediaView 之上

json - 我如何制作这个 jq 表达式,它适用于 jq >= 1.5,对于 < 1.5 的版本向后兼容/正确

bash:pm2:找不到命令

linux - 如何在命令行中使用单个命令运行一系列命令?

Mysql备份脚本

linux - bash tail 文件夹中的最新文件没有变量

bash - 如何通过命令行更新 GitHub 上拉取请求中的第一条评论?

c - argc 在 C 程序中给出错误的值