2015-10-31 61 views
0

我想要得到一個if或在bash中工作,但它不工作。收到以下錯誤Bash腳本if或

./install.sh: line 33: conditional binary operator expected 
./install.sh: line 33: syntax error near `$CURRENT_OS' 
./install.sh: line 33: `elif [[ $CURRENT_OS =~ "CYGWIN" || [ $CURRENT_OS =~ "MSYS" ]]; then' 

這裏是我當前的腳本

#!/bin/bash 

CURRENT_OS=$(uname) 

echo "Installing dotfiles" 

echo "Initializing submodule(s)" 
#git submodule update --init --recursive 

# Used for setting up a mac 
if [ $CURRENT_OS == "Darwin" ]; then 
    echo "Running OSX" 
elif [[ $CURRENT_OS =~ "CYGWIN" || [ $CURRENT_OS =~ "MSYS" ]]; then 
    echo $CURRENT_OS 
fi 

我甚至試過這種

elif [ $CURRENT_OS =~ "CYGWIN" || [ $CURRENT_OS =~ "MSYS" ]; then 

而且我得到這個錯誤

./install.sh: line 33: [: missing `]' 
./install.sh: line 33: [: =~: binary operator expected 
+1

你的'elif'有不平衡的括號。還有一個'[''而不是'''''你試過,'elif [$ CURRENT_OS =〜「CYGWIN」|| $ CURRENT_OS =〜「MSYS」];'? – lurker

+0

在發佈有關簡單語法錯誤之前,您應該嘗試http://shellcheck.net/。 – tripleee

回答

4

您有一個額外的[ b在elif分支的第二個條件之前。

elif [[ $CURRENT_OS =~ "CYGWIN" || [ $CURRENT_OS =~ "MSYS" ]]; then 
           ^-- Remove this 
+0

試過了,看看編輯上面 – jrock2004

+1

「第二個條件之前」 – Steven

1

爲了補充Steven's helpful answer

必須使用[[ ... ]]而不是[ ... ]使用bash的正則表達式匹配操作,=~

[ foo =~ o ] # !! FAILS, due to use of [ ... ]: "[: =~: binary operator expected" 

[[ foo =~ o ]] # OK - [[ ... ]] required 

使用習慣擊,你會得到以下條件:

if [[ $CURRENT_OS == 'Darwin' ]]; then 
    echo "Running OSX" 
elif [[ $CURRENT_OS =~ 'CYGWIN' || $CURRENT_OS =~ 'MSYS' ]]; then 
    echo "$CURRENT_OS" 
fi