[英]Bash scripting if or
我試圖獲得一個if或在bash中工作,但它不起作用。 出現以下錯誤
./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
您在elif
分支的第二個條件之前有一個額外的[
。
elif [[ $CURRENT_OS =~ "CYGWIN" || [ $CURRENT_OS =~ "MSYS" ]]; then
^ -- Remove this
為了補充史蒂文的有用答案 :
您必須使用[[ ... ]]
而不是[ ... ]
來使用Bash的正則表達式匹配運算符=~
:
[ foo =~ o ] # !! FAILS, due to use of [ ... ]: "[: =~: binary operator expected"
[[ foo =~ o ]] # OK - [[ ... ]] required
使用慣用的Bash,您將獲得以下條件:
if [[ $CURRENT_OS == 'Darwin' ]]; then
echo "Running OSX"
elif [[ $CURRENT_OS =~ 'CYGWIN' || $CURRENT_OS =~ 'MSYS' ]]; then
echo "$CURRENT_OS"
fi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.