简体   繁体   中英

Bash if or statement, command not found

I'm having issues with this line of a shell script:

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

I get "[: command not found" when running the script. I do not understand why.

+ '[' -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

I only get the error if running on 17.04, not 16.04. If I switch the statement around and check 17.04 first, it will break on 16.04, and not 17.04. Something in the second half of the statement is breaking.

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

There's your problem:

+ ' [' 17.04 == 17.04 ']'

You appear to have some kind of funky space character before the last [ . Delete and retype it to turn it into a regular ascii space.

When you copy-pasted the line here, or when viewing it via the non-raw github view, it's being translated into a regular space, so it's not present in your post. This is also why it works just fine when copy-pasted into another file.

Here's ShellCheck on the file as downloaded (and not copy-pasted) from github:

$ 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.

尝试这个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM